tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 3 level 0
thecoded.prof
5 months ago
24475294
e81c3da0
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+24
1 changed file
expand all
collapse all
unified
split
python
oct3
level0
date.py
+24
python/oct3/level0/date.py
···
1
1
+
def is_leap_year(yyyy: int) -> bool:
2
2
+
return not (yyyy % 4 or (yyyy % 400 and not yyyy % 100))
3
3
+
4
4
+
5
5
+
6
6
+
def sum_months(mm: int, yyyy: int) -> int:
7
7
+
months = [31, 29 if is_leap_year(yyyy) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8
8
+
return sum(months[:mm])
9
9
+
10
10
+
11
11
+
def nth_day(s: str) -> int:
12
12
+
"""Calculate the nth day since the beginning of the year."""
13
13
+
mm, dd, yyyy = map(int, [s[:2], s[2:4], s[4:]])
14
14
+
day_since_new_year = dd + sum_months(mm-1, yyyy)
15
15
+
return yyyy*1000 + day_since_new_year
16
16
+
17
17
+
18
18
+
print("we're testing the nth day function...", end='')
19
19
+
assert(nth_day("02072016") == 2016038)
20
20
+
assert(nth_day("12201996") == 1996355)
21
21
+
# assert(nth_day("abcdef") == None)
22
22
+
assert(nth_day("12312020") == 2020366)
23
23
+
assert(nth_day("12312021") == 2021365)
24
24
+
print("and it passes the example cases")