Last active
August 29, 2015 14:22
-
-
Save tancnle/3c3fecffb5f3925f1aea to your computer and use it in GitHub Desktop.
Convert arabic to roman numerals
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
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