|
9 | 9 | https://en.wikipedia.org/wiki/Doomsday_rule
|
10 | 10 | """
|
11 | 11 |
|
12 |
| -_doomsday_leap = [4,1,7,4,2,6,4,1,5,3,7,5] |
13 |
| -_doomsday_not_leap = [3,7,7,4,2,6,4,1,5,3,7,5] |
14 |
| -_week_day_names = { |
15 |
| - 0 : "Sunday", |
16 |
| - 1 : "Monday", |
17 |
| - 2 : "Tuesday", |
18 |
| - 3 : "Wednesday", |
19 |
| - 4 : "Thursday", |
20 |
| - 5 : "Friday", |
21 |
| - 6 : "Saturday" |
22 |
| -} |
| 12 | +_doomsday_leap = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] |
| 13 | +_doomsday_not_leap = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] |
| 14 | +_week_day_names = { |
| 15 | + 0: "Sunday", |
| 16 | + 1: "Monday", |
| 17 | + 2: "Tuesday", |
| 18 | + 3: "Wednesday", |
| 19 | + 4: "Thursday", |
| 20 | + 5: "Friday", |
| 21 | + 6: "Saturday", |
| 22 | +} |
23 | 23 |
|
24 | 24 |
|
25 | 25 | def get_week_day(year: int, month: int, day: int) -> str:
|
26 |
| - """ Returns the week-day name out of a given date. |
27 |
| - |
| 26 | + """Returns the week-day name out of a given date. |
| 27 | +
|
28 | 28 | >>> get_week_day(2020, 10, 24)
|
29 | 29 | Saturday
|
30 | 30 | >>> get_week_day(2017, 10, 24)
|
31 | 31 | Tuesday
|
32 |
| - |
| 32 | +
|
33 | 33 | """
|
34 | 34 | # minimal input check:
|
35 |
| - assert len(str(year)) > 2, 'Please supply year in YYYY format' |
36 |
| - assert 1 <= month <= 12, 'Invalid month value, please give a number between 1 to 12' |
37 |
| - assert 1 <= day <= 31, 'Invalid day value, please give a number between 1 to 31' |
| 35 | + assert len(str(year)) > 2, "Please supply year in YYYY format" |
| 36 | + assert 1 <= month <= 12, "Invalid month value, please give a number between 1 to 12" |
| 37 | + assert 1 <= day <= 31, "Invalid day value, please give a number between 1 to 31" |
38 | 38 |
|
39 | 39 | # Doomsday algorithm:
|
40 | 40 | century = year // 100
|
41 | 41 | century_anchor = (5 * (century % 4) + 2) % 7
|
42 | 42 | centurian = year % 100
|
43 | 43 | centurian_m = centurian % 12
|
44 |
| - dooms_day = ((centurian // 12) + centurian_m + |
45 |
| - (centurian_m // 4) + century_anchor) % 7 |
46 |
| - day_anchor = _doomsday_not_leap[month - 1] if (year % 4 != 0) or (centurian == 0 |
47 |
| - and (year % 400) == 0) else _doomsday_leap[month - 1] |
| 44 | + dooms_day = ( |
| 45 | + (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor |
| 46 | + ) % 7 |
| 47 | + day_anchor = ( |
| 48 | + _doomsday_not_leap[month - 1] |
| 49 | + if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) |
| 50 | + else _doomsday_leap[month - 1] |
| 51 | + ) |
48 | 52 | week_day = (dooms_day + day - day_anchor) % 7
|
49 | 53 | return _week_day_names[week_day]
|
50 | 54 |
|
51 | 55 |
|
52 |
| -if __name__ == '__main__': |
| 56 | +if __name__ == "__main__": |
53 | 57 | # unit-test:
|
54 | 58 | assert get_week_day(2020, 10, 24) == "Saturday"
|
0 commit comments