-
-
Save relaxedtomato/05c55518686226ba2c1cbbb47a33a68e 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 = [ | |
[1000, 'M'], | |
[500, 'D'], | |
[100, 'C'], | |
[50, 'L'], | |
[10, 'X'], | |
[5, 'V'], | |
[1, 'I'] | |
] | |
def to_roman(num) # RB: Inital Num | |
roman_string = "" | |
# RB: remainder_num = initial_num | |
# RB: When looping through data, make note of what you need to keep track of and what you need to change | |
@values.each do |numeral_pair| | |
# puts num | |
# RB: divisor instead of division | |
division = num / numeral_pair[0] # 6/5=1. 6/1=6 --> 1/1=1 | |
# RB: divsion will be an integer | |
if division > 0 #V IIIIII # RB: Is the num divisble by the numeral_pair[0] | |
roman_string = roman_string + numeral_pair[1] * division # RB: roman_string += numeral_pair[1] * divisor | |
# RB: Remainder of the Initial Num (what's left to convert to roman numerals) | |
num = num - numeral_pair[0] # RB: remainder_num -= numeral_pair[0]. | |
# RB: Also look into the modulus operator. where 6%5 = 1 | |
end | |
end | |
return roman_string | |
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(105) == "CV" | |
# 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