+24
python/oct3/level0/date.py
+24
python/oct3/level0/date.py
···
1
+
def is_leap_year(yyyy: int) -> bool:
2
+
return not (yyyy % 4 or (yyyy % 400 and not yyyy % 100))
3
+
4
+
5
+
6
+
def sum_months(mm: int, yyyy: int) -> int:
7
+
months = [31, 29 if is_leap_year(yyyy) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8
+
return sum(months[:mm])
9
+
10
+
11
+
def nth_day(s: str) -> int:
12
+
"""Calculate the nth day since the beginning of the year."""
13
+
mm, dd, yyyy = map(int, [s[:2], s[2:4], s[4:]])
14
+
day_since_new_year = dd + sum_months(mm-1, yyyy)
15
+
return yyyy*1000 + day_since_new_year
16
+
17
+
18
+
print("we're testing the nth day function...", end='')
19
+
assert(nth_day("02072016") == 2016038)
20
+
assert(nth_day("12201996") == 1996355)
21
+
# assert(nth_day("abcdef") == None)
22
+
assert(nth_day("12312020") == 2020366)
23
+
assert(nth_day("12312021") == 2021365)
24
+
print("and it passes the example cases")