-
Notifications
You must be signed in to change notification settings - Fork 0
/
p019.py
71 lines (53 loc) · 1.74 KB
/
p019.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
71
# Steve Beal
# Project Euler problem 19 solution
# 2/15/14
# You are given the following information, but you may prefer to do some
# research for yourself.
# 1 Jan 1900 was a Monday.
# Thirty days have September,
# April, June, and November.
# All the rest have thirty-one,
# Saving February alone,
# Which has twenty-eight, rain or shine
# And on leap years, twenty-nine
# A leap year occurs on any year evenly divisible by 4, but not on a century
# unless it is divisible by 400.
# How many Sundays fell on the first of the month during the twentieth century
# (1 Jan 1901 to 31 Dec 2000)?
reg_month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
leap_month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap_year(year):
if year % 400 == 0:
return True
elif year % 4 == 0 and year % 100 != 0:
return True
else:
return False
def num_twentieth_century_first_sundays():
# first sunday date (month indexed from 0, day indexed from 1)
day = 6
month = 0
year = 1901
first_sundays = 0
# gets the first sunday in 1901
while year < 2001:
# check if we're in feb of a leap year
if month == 1 and is_leap_year(year):
month_days = leap_month_days
else:
month_days = reg_month_days
# get the next sunday
day += 7
# advance the month
if day > month_days[month]:
day %= month_days[month]
month += 1
# advance the year
if month > 11:
month %= 12
year += 1
# count the sundays that are the first of the month
if day == 1:
first_sundays += 1
return first_sundays
print(num_twentieth_century_first_sundays())