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.
If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.
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.
In order to run the tests, issue the following command from the exercise directory:
For running the tests provided,
rebar3 is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.
In order to run the tests, you can issue the following command from the exercise directory.
$ rebar3 eunit
Each problem defines a macro
TEST_VERSION in the test file and
verifies that the solution defines and exports a function
returning that same value.
To make tests pass, add the following to your solution:
-export([test_version/0]). test_version() -> 1.
The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.
For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
-module(leap_tests). % This file is automatically generated from the exercises canonical data. -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). year_not_divisible_by_4_common_year_test() -> ?assertNot(leap:leap_year(2015)). year_divisible_by_4_not_divisible_by_100_leap_year_test() -> ?assert(leap:leap_year(1996)). year_divisible_by_100_not_divisible_by_400_common_year_test() -> ?assertNot(leap:leap_year(2100)). year_divisible_by_400_leap_year_test() -> ?assert(leap:leap_year(2000)). version_test() -> ?assertMatch(4, leap:test_version()).
-module(leap). -export([leap_year/1, test_version/0]). test_version() -> 3. leap_year(Year) -> Divides = fun(N) -> Year rem N == 0 end, Divides(4) and not Divides(100) or Divides(400).
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.