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.
isLeapYear(year: int) → bool
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func isLeapYear(year int) bool {
}
Worked examples
| Call | Result |
|---|---|
isLeapYear(2024) | true |
isLeapYear(2023) | false |
isLeapYear(1900) | false |
isLeapYear(2000) | true |
Hint
The 400 rule wins over the 100 rule, which wins over the 4 rule. Order the checks accordingly.
Reference solution in Go
func isLeapYear(year int) bool {
if year < 1 {
return false
}
if year%400 == 0 {
return true
}
if year%100 == 0 {
return false
}
return year%4 == 0
}