Browse Source

Solution

master
Julio Biason 4 years ago
parent
commit
8b8d856adc
  1. 41
      elixir/roman-numerals/lib/roman_numerals.ex
  2. 22
      elixir/roman-numerals/test/roman_numerals_test.exs

41
elixir/roman-numerals/lib/roman_numerals.ex

@ -1,8 +1,49 @@
defmodule RomanNumerals do defmodule RomanNumerals do
@letters [
%{:unitary => "I", :halfway => "V", :next => "X"},
%{:unitary => "X", :halfway => "L", :next => "C"},
%{:unitary => "C", :halfway => "D", :next => "M"},
%{:unitary => "M", :halfway => "" , :next => ""}
]
@doc """ @doc """
Convert the number to a roman number. Convert the number to a roman number.
""" """
@spec numeral(pos_integer) :: String.t() @spec numeral(pos_integer) :: String.t()
def numeral(number) do def numeral(number) do
numeral(number, @letters)
end
defp numeral(0, _letters) do
""
end
defp numeral(number, [current | next]) do
next_iteration = Integer.floor_div(number, 10)
lower_part = number - (next_iteration * 10)
numeral(next_iteration, next) <> letter(lower_part, current)
end
defp letter(single_value, map) do
case single_value do
0 -> ""
1 -> String.duplicate(map[:unitary], 1)
2 -> String.duplicate(map[:unitary], 2)
3 -> String.duplicate(map[:unitary], 3)
4 -> map[:unitary] <> map[:halfway]
5 -> map[:halfway]
6 -> map[:halfway] <> String.duplicate(map[:unitary], 1)
7 -> map[:halfway] <> String.duplicate(map[:unitary], 2)
8 -> map[:halfway] <> String.duplicate(map[:unitary], 3)
9 -> map[:unitary] <> map[:next]
# OR...
# x when x == 0 -> ""
# x when x >= 1 and x <= 3 -> String.duplicate(map[:unitary], x)
# x when x == 4 -> map[:unitary] <> map[:halfway]
# x when x == 5 -> map[:halfway]
# x when x > 5 and x <= 8 -> map[:halfway] <> String.duplicate(map[:unitary], x - 5)
# _ -> map[:unitary] <> map[:next]
end
end end
end end

22
elixir/roman-numerals/test/roman_numerals_test.exs

@ -1,92 +1,78 @@
defmodule RomanNumeralsTest do defmodule RomanNumeralsTest do
use ExUnit.Case use ExUnit.Case
# @tag :pending
test "1" do test "1" do
assert RomanNumerals.numeral(1) == "I" assert RomanNumerals.numeral(1) == "I"
end end
@tag :pending
test "2" do test "2" do
assert RomanNumerals.numeral(2) == "II" assert RomanNumerals.numeral(2) == "II"
end end
@tag :pending
test "3" do test "3" do
assert RomanNumerals.numeral(3) == "III" assert RomanNumerals.numeral(3) == "III"
end end
@tag :pending
test "4" do test "4" do
assert RomanNumerals.numeral(4) == "IV" assert RomanNumerals.numeral(4) == "IV"
end end
@tag :pending
test "5" do test "5" do
assert RomanNumerals.numeral(5) == "V" assert RomanNumerals.numeral(5) == "V"
end end
@tag :pending
test "6" do test "6" do
assert RomanNumerals.numeral(6) == "VI" assert RomanNumerals.numeral(6) == "VI"
end end
@tag :pending test "7" do
assert RomanNumerals.numeral(7) == "VII"
end
test "9" do test "9" do
assert RomanNumerals.numeral(9) == "IX" assert RomanNumerals.numeral(9) == "IX"
end end
@tag :pending
test "27" do test "27" do
assert RomanNumerals.numeral(27) == "XXVII" assert RomanNumerals.numeral(27) == "XXVII"
end end
@tag :pending
test "48" do test "48" do
assert RomanNumerals.numeral(48) == "XLVIII" assert RomanNumerals.numeral(48) == "XLVIII"
end end
@tag :pending
test "59" do test "59" do
assert RomanNumerals.numeral(59) == "LIX" assert RomanNumerals.numeral(59) == "LIX"
end end
@tag :pending
test "93" do test "93" do
assert RomanNumerals.numeral(93) == "XCIII" assert RomanNumerals.numeral(93) == "XCIII"
end end
@tag :pending
test "141" do test "141" do
assert RomanNumerals.numeral(141) == "CXLI" assert RomanNumerals.numeral(141) == "CXLI"
end end
@tag :pending
test "163" do test "163" do
assert RomanNumerals.numeral(163) == "CLXIII" assert RomanNumerals.numeral(163) == "CLXIII"
end end
@tag :pending
test "402" do test "402" do
assert RomanNumerals.numeral(402) == "CDII" assert RomanNumerals.numeral(402) == "CDII"
end end
@tag :pending
test "575" do test "575" do
assert RomanNumerals.numeral(575) == "DLXXV" assert RomanNumerals.numeral(575) == "DLXXV"
end end
@tag :pending
test "911" do test "911" do
assert RomanNumerals.numeral(911) == "CMXI" assert RomanNumerals.numeral(911) == "CMXI"
end end
@tag :pending
test "1024" do test "1024" do
assert RomanNumerals.numeral(1024) == "MXXIV" assert RomanNumerals.numeral(1024) == "MXXIV"
end end
@tag :pending
test "3000" do test "3000" do
assert RomanNumerals.numeral(3000) == "MMM" assert RomanNumerals.numeral(3000) == "MMM"
end end

Loading…
Cancel
Save