Created
April 30, 2015 14:50
-
-
Save levicole/9a7e926684b89452469c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
ROMAN_MAP = { | |
1 => "I", | |
4 => "IV", | |
5 => "V", | |
9 => "IX", | |
10 => "X", | |
40 => "XL", | |
50 => "L", | |
90 => "XC", | |
100 => "C", | |
400 => "CD", | |
500 => "D", | |
900 => "CM", | |
1000 => "M" } | |
def to_roman(i) | |
ROMAN_MAP.sort {|a,b| a[0] <=> b[0] }.reverse.collect do |k,v| | |
times, i = i.divmod(k) | |
v * times | |
end.join("") | |
end | |
def from_roman(s) | |
inverted_map = ROMAN_MAP.invert | |
s.split(//).collect do |n| | |
inverted_map[n] | |
end.inject() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment