In [54]:
"""
You are given the following information, but you may prefer to do some
research for yourself.
1 Jan 1900 was a Monday.
Thirty days has 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)?
"""
import datetime
def howManySundays(syear,smonth,sday,eyear,emonth,eday):
"""Returns the number of Sundays which fall on the first of a month between start date year,month,day and end
date year,month,day."""
start = datetime.date(year=syear,month=smonth,day=sday)
end = datetime.date(year=eyear,month=emonth,day=eday)
s = start.toordinal()
e = end.toordinal()
count = 0
for d in range(s,e+1):
# print(datetime.date.fromordinal(d).weekday()," ",datetime.date.fromordinal(d).day)
if (datetime.date.fromordinal(d).weekday() == 6 and datetime.date.fromordinal(d).day == 1):
count = count+1
return count
In [55]:
howManySundays(1901,1,1,2000,12,31)
Out[55]: