The Home for People Who Like to Flip Out and Write Code
Project Euler: Problem 19
I recently solved Problem 19 from Project Euler. Since I
don’t really enjoy date math, I decided to brute force it and use the
datetime module from the standard library.
123456789101112131415161718192021222324252627
fromdatetimeimportdatedefnext_first_of_month_in_20th():"""Generator to list every first of the month during the 20th century."""first=date(1901,1,1)yieldfirstwhilefirst.year<2001:iffirst.month==12:first=first.replace(year=first.year+1)first=first.replace(month=1)else:first=first.replace(month=first.month+1)yieldfirstdefmain():""" Solve `Problem 19`_ from Project Euler. .. _`Problem 19`: http://projecteuler.net/index.php?section=problems&id;=19 """returnlen([firstforfirstinnext_first_of_month_in_20th()iffirst.weekday()==6])if__name__=='__main__':printmain()