Returns the number of mondays that fall on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? >>> solution() 171
()
| 22 | |
| 23 | |
| 24 | def solution(): |
| 25 | """Returns the number of mondays that fall on the first of the month during |
| 26 | the twentieth century (1 Jan 1901 to 31 Dec 2000)? |
| 27 | |
| 28 | >>> solution() |
| 29 | 171 |
| 30 | """ |
| 31 | days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 32 | |
| 33 | day = 6 |
| 34 | month = 1 |
| 35 | year = 1901 |
| 36 | |
| 37 | sundays = 0 |
| 38 | |
| 39 | while year < 2001: |
| 40 | day += 7 |
| 41 | |
| 42 | if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): |
| 43 | if day > days_per_month[month - 1] and month != 2: |
| 44 | month += 1 |
| 45 | day = day - days_per_month[month - 2] |
| 46 | elif day > 29 and month == 2: |
| 47 | month += 1 |
| 48 | day = day - 29 |
| 49 | elif day > days_per_month[month - 1]: |
| 50 | month += 1 |
| 51 | day = day - days_per_month[month - 2] |
| 52 | |
| 53 | if month > 12: |
| 54 | year += 1 |
| 55 | month = 1 |
| 56 | |
| 57 | if year < 2001 and day == 1: |
| 58 | sundays += 1 |
| 59 | return sundays |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |