-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem019.py
More file actions
176 lines (142 loc) · 3.84 KB
/
problem019.py
File metadata and controls
176 lines (142 loc) · 3.84 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
DEBUG_GEN = 0
DEBUG_FUNC = 0
# Number of Days Month
# 31 1
# 28/29 2
# 31 3
# 30 4
# 31 5
# 30 6
# 31 7
# 31 8
# 30 9
# 31 10
# 30 11
# 31 12
MONTH = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec"
}
DAY = {
0: "Mon",
1: "Tue",
2: "Wed",
3: "Thu",
4: "Fri",
5: "Sat",
6: "Sun"
}
# a leap year occurs on any year evenly divisble by 4, but not on a century
# unless it is divisible by 400
def isLeap(year):
if year % 4 == 0:
if year % 400 == 0:
return True
if year % 100 == 0:
return False
return True
return False
# a function to return how many days are in a month, year
# month (1-12)
def total_days(month, year):
if month < 1 or month > 12:
print("warning: invalid month")
# special case for February on leap years
if month == 2:
if isLeap(year):
return 29
else:
return 28
# 30 days in April, June, September, November
if month == 4 or month == 6 or month == 9 or month == 11:
return 30
# 31 days all remaining months
return 31
# test total_days
if DEBUG_FUNC:
for y in range(1900,1905):
for m in range(1,13):
print(MONTH[m],y,"has",total_days(m,y),"days")
# test isLeap()
if DEBUG_FUNC:
nLeapYears = 0
for y in range(1900,2001):
if isLeap(y):
print(y, ": leap")
nLeapYears += 1
#else:
# print(y, ":")
print("total leap years:",nLeapYears)
print("1900 test:", isLeap(1900)) # Expect False
print("2000 test:", isLeap(2000)) # Expect True
print("--- Problem 19 ---")
if DEBUG_GEN:
for _ in range(15):
print(MONTH[month],day,year,DAY[date % 7])
date += 1
# Starting day: Monday, 1 Jan, 1901
date = 0
day = 1
month = 1
year = 1900
TOTAL_FIRST_SUNDAYS = 0
while year < 2001:
#print(MONTH[month],day,year,DAY[date % 7])
# check if today is Sunday and first of the month
# and year >= 1901 (!!!!!!!!!!!)
if DAY[date % 7] == "Sun" and day == 1 and year >= 1901:
print(MONTH[month],day,year,DAY[date % 7])
TOTAL_FIRST_SUNDAYS += 1
# calculate next day
day = day + 1
# check if we roll over to the next month
if day > total_days(month, year):
# reset day
day = 1
# update to next month
month = month + 1
# check if we roll over to the next year
if month > 12:
# reset month
month = 1
# update to next year
year = year + 1
date = date + 1
#print(MONTH[month],day,year,DAY[date % 7])
print("Total Sundays that fell on the first of the month:", TOTAL_FIRST_SUNDAYS)
# What day is May 23, 2025?
# Starting day: Monday, 1 Jan, 1901
date = 0
day = 1
month = 1
year = 1900
while year < 2026:
# break when we find out date of interest
if day == 23 and month == 5 and year == 2025:
break
# calculate next day
day = day + 1
# check if we roll over to the next month
if day > total_days(month, year):
# reset day
day = 1
# update to next month
month = month + 1
# check if we roll over to the next year
if month > 12:
# reset month
month = 1
# update to next year
year = year + 1
date = date + 1
print(MONTH[month],day,year,"is a",DAY[date % 7],"is the date I solved this")