aboutsummaryrefslogtreecommitdiff
path: root/python/019-counting_sundays.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/019-counting_sundays.py')
-rw-r--r--python/019-counting_sundays.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/python/019-counting_sundays.py b/python/019-counting_sundays.py
new file mode 100644
index 0000000..f018e05
--- /dev/null
+++ b/python/019-counting_sundays.py
@@ -0,0 +1,34 @@
+def month_days(index, leap_year):
+ if index == 2:
+ if leap_year:
+ return 29
+ return 28
+ if index in [4, 6, 9, 11]:
+ return 30
+ return 31
+
+
+def is_leap_year(year):
+ if str(year)[2:] == '00' and year % 400 != 0:
+ return False
+ if year % 4 == 0:
+ return True
+ return False
+
+def next_day(current):
+ if current == 7:
+ return 1
+ return current + 1
+
+months = [x for x in range(1, 13)]
+day = 1
+sundays_counter = 0
+
+for year in range(1900, 2001):
+ for month in months:
+ for d in range(month_days(month, is_leap_year(year))):
+ if day == 7 and d == 0 and year >= 1901:
+ sundays_counter += 1
+ day = next_day(day)
+
+print(sundays_counter)