Project Euler #19, Python -
i solving project euler #19:
how many sundays fell on first of month during twentieth century (1 jan 1901 31 dec 2000)?
and here code :
months = { "january": 31, "february" : 28, "march" : 31, "april" : 30, "may" : 31, "june" : 30, "july" : 31, "august" : 31, "september" : 30, "october" : 31, "november" : 30, "december" : 31} def countingsundays(): day = 1 sunday_count = 0 year in xrange(1901,2001): m in months: day += months[m] if year % 4 == 0 , m == "february": day += 1 if day % 7 == 0: sunday_count += 1 print "sundays:", sunday_count
the output of program 172 incorrect. searched answer 171. wanted know why getting 1 sunday ?
you're iterating on months
dict, expecting iterate in order of months, dicts aren't ordered, can months in wrong order.
since don't need month names, can make months
list of month lengths instead.
Comments
Post a Comment