From 2447529412cc7761d14e32340bf49438676b3b12 Mon Sep 17 00:00:00 2001 From: Samuel Shuert Date: Fri, 3 Oct 2025 16:01:38 +0000 Subject: [PATCH] feat: oct 3 level 0 --- python/oct3/level0/date.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 python/oct3/level0/date.py 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") -- 2.51.2