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
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.
%% Based on canonical data version 1.4.0 %% https://github.com/exercism/problem-specifications/raw/master/exercises/leap/canonical-data.json %% This file is automatically generated from the exercises canonical data. -module(leap_tests). -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). '1_year_not_divisible_by_4_common_year_test'() -> ?assertNot(leap:leap_year(2015)). '2_year_divisible_by_4_not_divisible_by_100_leap_year_test'() -> ?assert(leap:leap_year(1996)). '3_year_divisible_by_100_not_divisible_by_400_common_year_test'() -> ?assertNot(leap:leap_year(2100)). '4_year_divisible_by_400_leap_year_test'() -> ?assert(leap:leap_year(2000)). '5_year_divisible_by_200_not_divisible_by_400_common_year_test'() -> ?assertNot(leap:leap_year(1800)).
-module(leap). -export([leap_year/1]). leap_year(Year) when Year rem 400 =:= 0 -> true; leap_year(Year) when Year rem 100 =:= 0 -> false; leap_year(Year) when Year rem 4 =:= 0 -> true; leap_year(_Year) -> false.
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.