Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Leap in the Erlang Track

0
0
Genius
0
0
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.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

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.

Running tests

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

Test versioning

Each problem defines a macro TEST_VERSION in the test file and verifies that the solution defines and exports a function test_version 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.

Questions?

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.

Source

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

Submitting Incomplete Solutions

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

leap_tests.erl

-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).

What can you learn from this solution?

A huge amount can be learnt 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.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?