Python Challenge [15]

An introduction to time.

This challenge used a pretty discrete, task-oriented pair of libraries, as compared to most, since we’re clearly taking a look at a date. ‘time’, ‘datetime’ and ‘calendar’ seem like the three natural routes for exploration. A quick read-through of datetime (I went there first because I do php programming at the day job) gives us the notable function weekday() (or isoweekday() if you prefer) with the argument


datetime.weekday(year,month,day)

and returns an integer for the day of the week (0:6 :: Monday:Sunday). We’ll check through the list of years which fall in 1–6, so 1006 to 1996.


import datetime
for year in range(1006,1996,10):
    if datetime(year,1,26).weekday()==0:
        print year

That gives us a list of 10 years and here’s our leap for the puzzle. Since we’re looking at novel functions and calendars, might as well see if any of the dates are in leap years (grats to the forums), using:

calendar.isleap(year)

which, when we add it to our function, gives us


import datetime, calendar
for year in range(1006,1996,10):
    if datetime(year,1,26).weekday()==0:
        if calendar.isleap(year):
            print year

From here I hopped over to Wikipedia and came up with nothing helpful – time to take a look at the source again. Flowers for tomorrow? Gah, I’m using the wrong date. Though I just changed the date in my wikipedia search, we’ll correct the script while we’re here.


import datetime, calendar
for year in range(1006,1996,10):
    if datetime(year,1,27).weekday()==0:
        if calendar.isleap(year):
            print year

Wikipedia says… we’re bach in the game.

Python Documentation:
datetime
calendar

Ok, I totally apologize for the puns in this one.