From 239a0b50d0c40b6c58784c32f07d1a46cb03eef1 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 5 Sep 2025 00:49:30 +0800 Subject: [PATCH] feat: update leap_year.py --- leap_year.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) mode change 100644 => 100755 leap_year.py diff --git a/leap_year.py b/leap_year.py old mode 100644 new mode 100755 index 6a1002b..5fdea2b --- a/leap_year.py +++ b/leap_year.py @@ -4,7 +4,38 @@ def is_leap_year(year: int): return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 +def get_day_of_month(year, month: int) -> int: + match month: + case 1 | 3 | 5 | 7 | 8 | 10 | 12: + return 31 + case 4 | 6 | 9 | 11: + return 30 + case 2 if is_leap_year(year): + return 29 + case 2: + return 28 + case _: + raise Exception(f"invalid month: {month}") + + +def get_day_of_month2(year, month: int) -> int: + if [1, 3, 5, 7, 8, 10, 12].__contains__(month): + return 31 + elif [4, 6, 9, 11].__contains__(month): + return 30 + elif month == 2 and is_leap_year(year): + return 29 + elif month == 2: + return 28 + else: + raise Exception(f"invalid month: {month}") + + if __name__ == "__main__": print(1582, is_leap_year(1582)) print(2000, is_leap_year(2000)) print(2100, is_leap_year(2100)) + + print(2000, 1, '->', get_day_of_month(2000, 1), get_day_of_month2(2000, 1)) + print(2000, 2, '->', get_day_of_month(2000, 2), get_day_of_month2(2000, 2)) + print(2001, 2, '->', get_day_of_month(2001, 2), get_day_of_month2(2001, 2))