Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created February 22, 2013 00:26
Show Gist options
  • Save jimweirich/5009762 to your computer and use it in GitHub Desktop.
Save jimweirich/5009762 to your computer and use it in GitHub Desktop.
Another roman numeral converter solution
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