Created
August 8, 2018 14:55
-
-
Save arthurstomp/3b55a19e4ef340abf10ecc5de9eb3f7d to your computer and use it in GitHub Desktop.
Int to Romans
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
class Romans | |
@@romans = { | |
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' | |
} | |
def self.parse_int(i) | |
result = [] | |
desc_keys = @@romans.keys.sort.reverse | |
while i > 0 do | |
desc_keys.map do |r| | |
d = (i / r).to_i | |
next if d <= 0 | |
result.push(@@romans[r] * d) | |
i -= r * d | |
end | |
end | |
return result | |
end | |
end | |
puts Romans.parse_int(2015) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment