Python Implementation of Problem 19

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 19

This one ended up being very easy thanks to the datetime library

Problem:

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)?

python.src.p0019.monthly_iterator(years: range, months: range = range(1, 13), day: int = 1) Iterable[date]
python.src.p0019.main() int
 1"""
 2Project Euler Problem 19
 3
 4This one ended up being very easy thanks to the datetime library
 5
 6Problem:
 7
 8You are given the following information, but you may prefer to do some research
 9for yourself.
10
11    1 Jan 1900 was a Monday.
12    Thirty days has September,
13    April, June and November.
14    All the rest have thirty-one,
15    Saving February alone,
16    Which has twenty-eight, rain or shine.
17    And on leap years, twenty-nine.
18    A leap year occurs on any year evenly divisible by 4, but not on a century
19    unless it is divisible by 400.
20
21How many Sundays fell on the first of the month during the twentieth century
22(1 Jan 1901 to 31 Dec 2000)?
23"""
24from datetime import date
25from typing import Iterable
26
27
28def monthly_iterator(years: range, months: range = range(1, 13), day: int = 1) -> Iterable[date]:
29    for x in years:
30        for y in range(1, 13):
31            _day = date(x, y, day)
32            if _day.weekday() == 6:
33                yield _day
34
35
36def main() -> int:
37    return sum(1 for _ in monthly_iterator(years=range(1901, 2001)))

Tags: calendar, combinatorics