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.
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.
Refer to the Installing Elm page for information about installing elm.
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:
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
calls from each test until you get them all passing!
JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp
It is possible to submit an incomplete solution so you can see how others have completed the exercise.
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 = year |> modBy leapYear |> (==) 0 isNonCenturialYear : Int -> Bool isNonCenturialYear year = year |> modBy centurialYear |> (==) 0 |> not isLeapCycleYear : Int -> Bool isLeapCycleYear year = let leapCycleLength = leapYear * centurialYear in year |> modBy leapCycleLength |> (==) 0 leapYear : Int leapYear = 4 centurialYear : Int centurialYear = 100
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.