-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path019 - Counting Sundays.py
70 lines (51 loc) · 1.67 KB
/
019 - Counting Sundays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
__author__ = 'Mathias'
import timeit
class BidirectionalDict(dict):
def __setitem__(self, key, val):
dict.__setitem__(self, key, val)
dict.__setitem__(self, val, key)
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"]
week_dict = BidirectionalDict()
for i, day in enumerate(days):
week_dict[day] = i
month_dict = {}
for month in months:
if month == "September" or "April" or "June" or "November":
month_dict[month] = 30
elif month == "February":
month_dict[month] = 28
else:
month_dict[month] = 31
def given_day_X_after_Y_days_it_is(x, y):
week_nb = week_dict.get(x)
new_week_nb = (y + week_nb) % 7
return week_dict.get(new_week_nb)
def is_leap_year(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
start = timeit.default_timer()
start_day = "Monday"
start_year = 1900
end_year = 2000
curr_day = start_day
curr_year = start_year
total_sundays = 0
while curr_year <= end_year:
for month in months:
if month == "February" and is_leap_year(curr_year):
curr_day = given_day_X_after_Y_days_it_is(curr_day, month_dict.get(month) + 1)
else:
curr_day = given_day_X_after_Y_days_it_is(curr_day, month_dict.get(month))
if curr_year != 1900 and curr_day == "Sunday":
total_sundays += 1
curr_year += 1
print(total_sundays)
print(timeit.default_timer() - start)