diff --git a/python/oct3/level0/date.py b/python/oct3/level0/date.py new file mode 100644 index 0000000..c121556 --- /dev/null +++ b/python/oct3/level0/date.py @@ -0,0 +1,24 @@ +def is_leap_year(yyyy: int) -> bool: + return not (yyyy % 4 or (yyyy % 400 and not yyyy % 100)) + + + +def sum_months(mm: int, yyyy: int) -> int: + months = [31, 29 if is_leap_year(yyyy) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + return sum(months[:mm]) + + +def nth_day(s: str) -> int: + """Calculate the nth day since the beginning of the year.""" + mm, dd, yyyy = map(int, [s[:2], s[2:4], s[4:]]) + day_since_new_year = dd + sum_months(mm-1, yyyy) + return yyyy*1000 + day_since_new_year + + +print("we're testing the nth day function...", end='') +assert(nth_day("02072016") == 2016038) +assert(nth_day("12201996") == 1996355) +# assert(nth_day("abcdef") == None) +assert(nth_day("12312020") == 2020366) +assert(nth_day("12312021") == 2021365) +print("and it passes the example cases")