Skip to content

Instantly share code, notes, and snippets.

@opensussex
Last active April 11, 2018 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opensussex/d10b9a418f7f99116840ee007d73a238 to your computer and use it in GitHub Desktop.
Save opensussex/d10b9a418f7f99116840ee007d73a238 to your computer and use it in GitHub Desktop.
having a go at creating a module to work out the number of boxes are needed for the number of matches past in - this is for the learning functional programming with elixir book
defmodule MatchStickFactory do
def boxes(number_of_matches) do
number_of_matches_in_big = 50;
number_of_matches_in_medium = 20;
number_of_matches_in_small = 5;
number_of_matches_remaining = number_of_matches;
{number_of_big_boxes, number_of_matches_remaining} = divide(number_of_matches_remaining, number_of_matches_in_big)
{number_of_medium_boxes, number_of_matches_remaining} = divide(number_of_matches_remaining, number_of_matches_in_medium)
{number_of_small_boxes, number_of_matches_remaining} = divide(number_of_matches_remaining, number_of_matches_in_small)
%{
big: number_of_big_boxes,
medium: number_of_medium_boxes,
small: number_of_small_boxes,
remaining: number_of_matches_remaining
}
end
def divide(dividend, divisor) do
{div(dividend, divisor), rem(dividend,divisor)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment