From 7b624de8e3e3637a07364f992c1d7e4185e4a872 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 11 Aug 2019 18:42:52 +0200 Subject: initial commit --- python/19-counting_sundays.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python/19-counting_sundays.py (limited to 'python/19-counting_sundays.py') diff --git a/python/19-counting_sundays.py b/python/19-counting_sundays.py new file mode 100644 index 0000000..f018e05 --- /dev/null +++ b/python/19-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) -- cgit