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 code you have to write is located inside the
src/ directory of the exercise.
Elm automatically installs packages dependencies the first time you run the tests
so we can start by running the tests from the exercise directory with:
To automatically run tests again when you save changes:
$ elm-test --watch
As you work your way through the tests suite in the file
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 = isDivisibleBy 4 year && not (isDivisibleBy 100 year && not (isDivisibleBy 400 year) ) isDivisibleBy divisor n = modBy divisor n == 0
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.