Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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)?
- def isLeap(year):
- """Return True for leap years, False for non-leap years."""
- return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
- def add2Cal(days):
- day = 1
- weekDay = calender[-1][3]
- weekDayIndex = weekDays.index(weekDay)
- while day <= days:
- calender.append([day,month,year,weekDays[((weekDayIndex+1)%7)]])
- day += 1
- weekDayIndex = ((weekDayIndex+1)%7)
- years = range(1900,2001)
- months = ['Jan','Feb','March','April','May','June',\
- 'July','August','Sept','Oct','Nov','Dec']
- weekDays = ['Mon','Tue','Wed','Thr','Fri','Sat','Sun']
- calender = [[0,0,0,'Sun']]
- for year in years:
- for month in months:
- if month in ['Sept','April','June','Nov']:
- add2Cal(30)
- elif month == 'Feb' and isLeap(year):
- add2Cal(29)
- elif month == 'Feb':
- add2Cal(28)
- else:
- add2Cal(31)
- count = 0
- for days in calender:
- if days[2] > 1900 and days[0] == 1 and days[3] == 'Sun':
- count += 1
- print count
Advertisement
Add Comment
Please, Sign In to add comment