Skip to content

Instantly share code, notes, and snippets.

@vorce
Last active December 15, 2015 07:59
Show Gist options
  • Save vorce/5227527 to your computer and use it in GitHub Desktop.
Save vorce/5227527 to your computer and use it in GitHub Desktop.
Roman numerals kata in Elixir lang. My very first use of the language >_<
Code.require_file "../test_helper.exs", __FILE__
defmodule ElixirRoman1Test do
@moduledoc "Tests number to roman numerals"
use ExUnit.Case
defp romans_list do
HashDict.new [ {10, "X"}, {9, "IX"}, {5, "V"},
{4, "IV"}, {1, "I"}]
end
def roman(0) do
""
end
@doc "Returns the roman number representation of nr"
def roman(nr) do
{num, rom} = get_biggest_roman_less_than(nr)
rom <> roman(nr - num)
end
defp get_biggest_roman_less_than(nr) do
[{num, rom} | _ ] = Enum.reverse Enum.filter romans_list,
fn({key, value}) -> nr >= key end
{num, rom}
end
test "roman 0 should return empty string" do
assert "" == roman(0)
end
test "roman 1 should return I" do
assert "I" == roman(1)
end
test "roman 2 should return II" do
assert "II" == roman(2)
end
test "roman 5 should return V" do
assert "V" == roman(5)
end
test "roman 34 should return XXXIV" do
assert "XXXIV" == roman(34)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment