# paulfioravanti's solution

## to Leap in the Elm Track

Published at Jun 20 2019 · 0 comments
Instructions
Test suite
Solution

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

``````on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
``````

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Expect
import Leap
import Test exposing (..)

tests : Test
tests =
describe "Leap"
[ test "leap year" <|
\() -> Expect.equal True (Leap.isLeapYear 1996)
, skip <|
test "non-leap year" <|
\() -> Expect.equal False (Leap.isLeapYear 1997)
, skip <|
test "non-leap even year" <|
\() -> Expect.equal False (Leap.isLeapYear 1998)
, skip <|
test "century" <|
\() -> Expect.equal False (Leap.isLeapYear 1900)
, skip <|
test "second century" <|
\() -> Expect.equal False (Leap.isLeapYear 1800)
, skip <|
test "fourth century" <|
\() -> Expect.equal True (Leap.isLeapYear 2400)
, skip <|
test "y2k" <|
\() -> Expect.equal True (Leap.isLeapYear 2000)
]``````
``````module Leap exposing (isLeapYear)

isLeapYear : Int -> Bool
isLeapYear year =
isLeap year && (isNonCenturialYear year || isLeapCycleYear year)

-- PRIVATE

isLeap : Int -> Bool
isLeap year =
modBy leapYear year == 0

isNonCenturialYear : Int -> Bool
isNonCenturialYear year =
not (modBy centurialYear year == 0)

isLeapCycleYear : Int -> Bool
isLeapCycleYear year =
let
leapCycleLength =
leapYear * centurialYear
in
modBy leapCycleLength year == 0

leapYear : Int
leapYear =
4

centurialYear : Int
centurialYear =
100``````