CMU Coding Bootcamp
1def dayOfWeek(month: int, day: int, year: int) -> int:
2 """Return the day of the week for a given date."""
3 m = month if month > 2 else month + 12
4 y = year - 1 if month <= 2 else year
5 n = (day + 2 * m + (3 * (m + 1)) // 5 + y + y // 4 - y // 100 + y // 400 + 2) % 7
6 return n if n != 0 else 7
7
8
9print("Testing dayOfWeek()...")
10# On 6/15/1215, the Magna Carta was signed on a Monday!
11assert dayOfWeek(6, 15, 1215) == 2
12# On 3/11/1952, the author Douglas Adams was born on a Tuesday!
13assert dayOfWeek(3, 11, 1952) == 3
14# on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
15assert dayOfWeek(4, 12, 1961) == 4
16# On 7/4/1776, the Declaration of Independence was signed on a Thursday!
17assert dayOfWeek(7, 4, 1776) == 5
18# on 1/2/1920, Isaac Asimov was born on a Friday!
19assert dayOfWeek(1, 2, 1920) == 6
20# on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
21assert dayOfWeek(10, 11, 1975) == 7
22# On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
23assert dayOfWeek(2, 5, 2006) == 1
24print("Passed!")