Day of the Week LeetCode solution in Python

1
class Solution:
2
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
3
LOWEST_DAY, LOWEST_MONTH, LOWEST_YEAR, DAY = 1, 1, 1971, 5
4
DAYS = (
5
"Sunday",
6
"Monday",
7
"Tuesday",
8
"Wednesday",
9
"Thursday",
10
"Friday",
11
"Saturday",
12
)
13

14
difference = self.daysBetweenDates(
15
(LOWEST_DAY, LOWEST_MONTH, LOWEST_YEAR), (day, month, year)
16
)
17
return DAYS[(difference + DAY) % 7]
18

19
def daysBetweenDates(self, date1: tuple, date2: tuple) -> int:
20
LOWEST_YEAR = 1971
21

22
def daysSinceLowest(date: tuple) -> int:
23
day, month, year = date
24

25
isLeapYear = lambda x: (
26
1 if (x % 4 == 0 and x % 100 != 0) or x % 400 == 0 else 0
27
)
28

29
days: int = 0
30
# days between the LOWEST_YEAR and year
31
days += 365 * (year - LOWEST_YEAR) + sum(
32
map(isLeapYear, range(LOWEST_YEAR, year))
33
)
34
# days between year and exact date
35
daysInMonth = (
36
31,
37
28 + isLeapYear(year),
38
31,
39
30,
40
31,
41
30,
42
31,
43
31,
44
30,
45
31,
46
30,
47
31,
48
)
49
days += sum(daysInMonth[: month - 1]) + day
50
return days
51

52
return abs(daysSinceLowest(date1) - daysSinceLowest(date2))

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0