Skip to content

Instantly share code, notes, and snippets.

@kaddopur
Last active January 15, 2017 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaddopur/af400e00a1811f0ee777fa43a0b6c11f to your computer and use it in GitHub Desktop.
Save kaddopur/af400e00a1811f0ee777fa43a0b6c11f to your computer and use it in GitHub Desktop.
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
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