feat: update leap_year.py
This commit is contained in:
31
leap_year.py
Normal file → Executable file
31
leap_year.py
Normal file → Executable file
@@ -4,7 +4,38 @@ def is_leap_year(year: int):
|
|||||||
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
|
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__":
|
if __name__ == "__main__":
|
||||||
print(1582, is_leap_year(1582))
|
print(1582, is_leap_year(1582))
|
||||||
print(2000, is_leap_year(2000))
|
print(2000, is_leap_year(2000))
|
||||||
print(2100, is_leap_year(2100))
|
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))
|
||||||
|
|||||||
Reference in New Issue
Block a user