/roman_numerals.rb Secret
Last active
August 29, 2015 14:24
-
-
Save rh0delta/2563fbb57085c7bdc1b6 to your computer and use it in GitHub Desktop.
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
VALUES = [ | |
["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] | |
] | |
def to_roman(num) | |
# Your code here | |
roman = "" | |
VALUES.each do |pair| | |
letter = pair[0] | |
value = pair[1] | |
roman += letter*(num / value) | |
num = num % value | |
end | |
return roman | |
end | |
# Drive code... this should print out trues. | |
puts "My totally sweet testing script" | |
puts "" | |
puts "input | expected | actual" | |
puts "------|----------|-------" | |
puts "4 | IV | #{to_roman(4)}" | |
puts "9 | IX | #{to_roman(9)}" | |
puts "13 | XIII | #{to_roman(13)}" | |
puts "1453 | MCDLIII | #{to_roman(1453)}" | |
puts "1646 | MDCXLVI | #{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