Skip to content

Instantly share code, notes, and snippets.

@levicole
Created April 30, 2015 14:50
Show Gist options
  • Save levicole/9a7e926684b89452469c to your computer and use it in GitHub Desktop.
Save levicole/9a7e926684b89452469c to your computer and use it in GitHub Desktop.
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