Skip to content

Instantly share code, notes, and snippets.

@tancnle
Last active August 29, 2015 14:22
Show Gist options
  • Save tancnle/3c3fecffb5f3925f1aea to your computer and use it in GitHub Desktop.
Save tancnle/3c3fecffb5f3925f1aea to your computer and use it in GitHub Desktop.
Convert arabic to roman numerals
def possible_reps(input)
lambda {|k,_| k <= input}
end
def build_roman(input)
lambda do |result, (arabic, roman)|
if (input >= arabic)
quotient, input = input.divmod(arabic)
result << roman*quotient
end
result
end
end
def romanize(input)
arabic_roman = {
1000 => 'M',
900 => 'CM',
500 => 'D',
400 => 'CD',
100 => 'C',
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
1 => 'I'
}.freeze
arabic_roman.select( &possible_reps(input) ).reduce( [], &build_roman(input) ).join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment