Is it a leap year
A billing schedule needs to know whether February has 29 days.
- A year divisible by 4 is a leap year, except that a year divisible by 100 is not, unless it is also divisible by 400.
- Anything below year 1 is not a year at all.
is_leap_year(year: int) → bool
Where you start
def is_leap_year(year: int) -> bool:
Worked examples
| Call | Result |
|---|---|
is_leap_year(2024) | True |
is_leap_year(2023) | False |
is_leap_year(1900) | False |
is_leap_year(2000) | True |
Hint
The 400 rule wins over the 100 rule, which wins over the 4 rule. Order the checks accordingly.
Reference solution in Python
def is_leap_year(year: int) -> bool:
if year < 1:
return False
if year % 400 == 0:
return True
if year % 100 == 0:
return False
return year % 4 == 0