Created
May 23, 2019 14:28
-
-
Save BrandonMathis/f1457a0704ab72d0fb7f8453cd070bd9 to your computer and use it in GitHub Desktop.
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 Romanize do | |
@map [ | |
{ "D", 500 }, | |
{ "CD", 400 }, | |
{ "C", 100 }, | |
{ "L", 50 }, | |
{ "XL", 40 }, | |
{ "X", 10 }, | |
{ "IX", 9 }, | |
{ "V", 5 }, | |
{ "IV", 4 }, | |
{ "I", 1 } | |
] | |
def convert(decimal) do | |
convert(decimal, @map) | |
end | |
defp convert(0, _numerals), do: "" | |
defp convert(decimal, [{ roman, arabic } | tail ]) when decimal >= arabic do | |
roman <> convert(decimal - arabic, [{ roman, arabic } | tail]) | |
end | |
defp convert(decimal, [{ _roman, arabic } | tail]) when decimal < arabic do | |
convert(decimal, tail) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment