Introduction
A leap year (in the Gregorian calendar) occurs:
- In every year that is evenly divisible by 4.
- Unless the year is evenly divisible by 100, in which case it’s only a leap year if the year is also evenly divisible by 400.
Some examples:
- 1997 was not a leap year as it’s not divisible by 4.
- 1900 was not a leap year as it’s not divisible by 400.
- 2000 was a leap year!
For a delightful, four-minute explanation of the whole phenomenon of leap years, check out [this YouTube video](https://www.youtube.com/watch?v=xX96xng7sAE).
Instructions
Instructions
Your task is to determine whether a given year is a leap year.
Dig Deeper
Boolean chain
Chain of boolean expressions
// Package leap is a small library for determing if the passed in year is a leap year.
package leap
// IsLeapYear returns if the passed in year is a leap year.
func IsLeapYear(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
The first boolean expression uses the modulus operator to check if the year is evenly divided by 4.
- If the year is not evenly divisible by
4, then the chain will “short circuit” due to the next operator being a logical AND (&&),
and will return false.
- If the year is evenly divisible by
4, then the year is checked to not be evenly divisible by 100.
- If the year is not evenly divisible by
100, then the expression is true and the chain will “short-circuit” to return true,
since the next operator is a logical OR (||).
- If the year is evenly divisible by
100, then the expression is false, and the returned value from the chain will be if the year is evenly divisible by 400.
This approach exhausts after a maximum of three checks.
| year | year % 4 == 0 | year % 100 != 0 | year % 400 == 0 | is leap year |
|---|
| 2020 | true | true | not evaluated | true |
| 2019 | false | not evaluated | not evaluated | false |
| 2000 | true | false | true | true |
| 1900 | true | false | false | false |
Maximum two checks
Maximum of two checks
// Package leap is a small library for determing if the passed in year is a leap year.
package leap
// IsLeapYear returns if the passed in year is a leap year.
func IsLeapYear(year int) bool {
if year%100 == 0 {
return year%400 == 0
} else {
return year%4 == 0
}
}
This approach uses a maximum of two checks to determine if a year is a leap year.
It starts by testing the outlier condition of the year being evenly divisible by 100.
It does this by using the modulus operator.
If the year is evenly divisible by 100, then the expression is true, and the function returns if the year is evenly divisible by 400.
If the year is not evenly divisible by 100, then the expression is false, and the function returns if the year is evenly divisible by 4.
| year | year % 100 == 0 | year % 400 == 0 | year % 4 == 0 | is leap year |
|---|
| 2020 | false | not evaluated | true | true |
| 2019 | false | not evaluated | false | false |
| 2000 | true | true | not evaluated | true |
| 1900 | true | false | not evaluated | false |
Although it uses a maximum of only two checks, this approach tests an outlier condition of the year being evenly divisible by 100 first,
which is less likely than the year being evenly divisible by 4.
time addition
time.Add()
// Package leap is a small library for determing if the passed in year is a leap year.
package leap
import "time"
// IsLeapYear returns if the passed in year is a leap year.
func IsLeapYear(year int) bool {
return time.Date(year, time.February, 28, 0, 0, 0, 0, time.UTC).Add(time.Duration(time.Hour*24)).Day() == 29
}
This approach may be considered a “cheat” for this exercise.
By adding a day to February 28th for the year, you can see if the new day is the 29th or the 1st.
If it is the 29th, then the year is a leap year.
This is done by using the time.Add() and time.Day() functions.
Source: Exercism go/leap