You are given the following information, but you may prefer to do some research for yourself.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Accumulate the total days of each month since 1 Jan 1900, and check if the first day of this month is sunday.
month_day_norm = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_day_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap(y):
return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
is_leap(1901), is_leap(1904), is_leap(2000), is_leap(1900)
def solve():
day_cnt = 0
sunday_first_cnt = 0
for y in range(1900, 2001):
if is_leap(y):
month_day = month_day_leap
else:
month_day = month_day_norm
for m in range(12):
if y != 1900 and day_cnt % 7 == 6:
sunday_first_cnt += 1
day_cnt += month_day[m]
return sunday_first_cnt
solve()