Last active
April 11, 2018 11:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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