Created
August 12, 2016 13:32
-
-
Save ddevlin/7fca4b62953a43445ceecd8cca4efb08 to your computer and use it in GitHub Desktop.
Berlin.ex 11-08-2016 Roman Numerals Kata
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 Katas do | |
def numeral_to_int(numeral, numbers \\ []) do | |
case numeral do | |
"CM" <> rest -> numeral_to_int(rest, numbers ++ [900]) | |
"CD" <> rest -> numeral_to_int(rest, numbers ++ [400]) | |
"XC" <> rest -> numeral_to_int(rest, numbers ++ [90]) | |
"XL" <> rest -> numeral_to_int(rest, numbers ++ [40]) | |
"IX" <> rest -> numeral_to_int(rest, numbers ++ [9]) | |
"IV" <> rest -> numeral_to_int(rest, numbers ++ [4]) | |
"M" <> rest -> numeral_to_int(rest, numbers ++ [1000]) | |
"D" <> rest -> numeral_to_int(rest, numbers ++ [500]) | |
"C" <> rest -> numeral_to_int(rest, numbers ++ [100]) | |
"L" <> rest -> numeral_to_int(rest, numbers ++ [50]) | |
"X" <> rest -> numeral_to_int(rest, numbers ++ [10]) | |
"V" <> rest -> numeral_to_int(rest, numbers ++ [5]) | |
"I" <> rest -> numeral_to_int(rest, numbers ++ [1]) | |
"" -> Enum.sum(numbers) | |
end | |
end | |
def int_to_numeral(number) do | |
steps = [ | |
{"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100}, {"XC", 90}, | |
{"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1} | |
] | |
int_to_numeral_step(number, "", steps) | |
end | |
defp int_to_numeral_step(number, numeral, [{symbol, divisor} | tail]) do | |
numeral = numeral <> String.duplicate(symbol, div(number, divisor)) | |
remainder = rem(number, divisor) | |
int_to_numeral_step(remainder, numeral, tail) | |
end | |
defp int_to_numeral_step(_, numeral, _), do: numeral | |
end |
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 KatasTest do | |
use ExUnit.Case | |
test "numeral_to_int" do | |
assert Katas.numeral_to_int("I") == 1 | |
assert Katas.numeral_to_int("II") == 2 | |
assert Katas.numeral_to_int("III") == 3 | |
assert Katas.numeral_to_int("IV") == 4 | |
assert Katas.numeral_to_int("V") == 5 | |
assert Katas.numeral_to_int("VI") == 6 | |
assert Katas.numeral_to_int("VIII") == 8 | |
assert Katas.numeral_to_int("IX") == 9 | |
assert Katas.numeral_to_int("X") == 10 | |
assert Katas.numeral_to_int("XIII") == 13 | |
assert Katas.numeral_to_int("XV") == 15 | |
assert Katas.numeral_to_int("XVIII") == 18 | |
assert Katas.numeral_to_int("XL") == 40 | |
assert Katas.numeral_to_int("L") == 50 | |
assert Katas.numeral_to_int("MCMXC") == 1990 | |
assert Katas.numeral_to_int("MCMXCIX") == 1999 | |
assert Katas.numeral_to_int("MMXVI") == 2016 | |
end | |
test "int_to_numeral" do | |
assert Katas.int_to_numeral(1) == "I" | |
assert Katas.int_to_numeral(2) == "II" | |
assert Katas.int_to_numeral(3) == "III" | |
assert Katas.int_to_numeral(4) == "IV" | |
assert Katas.int_to_numeral(5) == "V" | |
assert Katas.int_to_numeral(6) == "VI" | |
assert Katas.int_to_numeral(8) == "VIII" | |
assert Katas.int_to_numeral(9) == "IX" | |
assert Katas.int_to_numeral(10) == "X" | |
assert Katas.int_to_numeral(13) == "XIII" | |
assert Katas.int_to_numeral(15) == "XV" | |
assert Katas.int_to_numeral(18) == "XVIII" | |
assert Katas.int_to_numeral(40) == "XL" | |
assert Katas.int_to_numeral(50) == "L" | |
assert Katas.int_to_numeral(1990) == "MCMXC" | |
assert Katas.int_to_numeral(1999) == "MCMXCIX" | |
assert Katas.int_to_numeral(2000) == "MM" | |
assert Katas.int_to_numeral(2016) == "MMXVI" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment