aboutsummaryrefslogtreecommitdiff
path: root/challenge-237/steven-wilson/python/ch-01.py
blob: 6da63f3aa6a163fe818d3bb602ac09ab66693883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3

from datetime import date, timedelta


def seize_the_day(year, month, week, dow):
    """ Given a year, a month, a week of month, and a day of week 
    (1 (Mon) .. 7 (Sun)), return the day of month, 0 is returned if not 
    possible
    >>> seize_the_day(2024, 4, 3, 2)
    16
    >>> seize_the_day(2025, 10, 2, 4)
    9
    >>> seize_the_day(2026, 8, 5, 3)
    0
    """
    d = date(year, month, (1 + (week - 1) * 7))
    while(d.isoweekday() != dow):
        d = d + timedelta(days=1)
    if(d.month == month):
        return d.day
    else:
        return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()