Created
February 22, 2013 00:26
-
-
Save jimweirich/5009762 to your computer and use it in GitHub Desktop.
Another roman numeral converter solution
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 RomanNumeralConverter | |
CONVERTS = { | |
"I" => 1, | |
"V" => 5, | |
"X" => 10, | |
"L" => 50, | |
"C" => 100, | |
"D" => 500, | |
"M" => 1000, | |
} | |
def convert(input) | |
sum = 0 | |
values = input.chars.map { |c| CONVERTS[c] } | |
while v = values.shift | |
if ! values.empty? && v < values.first | |
sum += values.first - v | |
values.shift | |
else | |
sum += v | |
end | |
end | |
sum.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment