Forked from nextacademy-private/roman_numerals.rb
Last active
March 27, 2016 05:00
-
-
Save samkahchiin/85c547f8293a6f8a9927 to your computer and use it in GitHub Desktop.
Roman Numerals
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
def to_roman(num) | |
# Your code here | |
roman_hash = { "M" => 1000, | |
"CM" => 900, | |
"D" => 500, | |
"CD" => 400, | |
"C" => 100, | |
"XC" => 90, | |
"L" => 50, | |
"XL" => 40, | |
"X" => 10, | |
"IX" => 9, | |
"V" => 5, | |
"IV" => 4, | |
"I" => 1 | |
} | |
roman = "" | |
while num != 0 | |
roman_hash.each_pair do |key,value| | |
if num / value >= 1 | |
roman += key * (num / value) | |
num = num % value | |
end | |
end | |
end | |
roman | |
end | |
# Drive code... this should print out trues. | |
puts to_roman(1) == "I" | |
puts to_roman(3) == "III" | |
puts to_roman(6) == "VI" | |
puts to_roman(1646) | |
# TODO: what other cases could you add to ensure your to_roman method is working? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment