Last active
January 15, 2017 09:27
-
-
Save kaddopur/af400e00a1811f0ee777fa43a0b6c11f 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 Roman do | |
@roman_symbols [ | |
["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], | |
]; | |
def numeral(0), do: "" | |
def numeral(n) do | |
[symbol, value] = Enum.filter(@roman_symbols, fn x -> n >= Enum.at(x, 1) end) |> List.first | |
symbol <> numeral(n - value) | |
end | |
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
const romanSymbols = [ | |
['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], | |
]; | |
function romanNumeral(n) { | |
if (n === 0) { | |
return ''; | |
} | |
const targetSymbol = romanSymbols.filter(symbol => n >= symbol[1])[0]; | |
return targetSymbol[0] + romanNumeral(n - targetSymbol[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment