Skip to content

Instantly share code, notes, and snippets.

@stupeters187
Last active March 30, 2016 23:38
Show Gist options
  • Save stupeters187/b6ddc3f21edc3a0d4d392b1bf060ebe7 to your computer and use it in GitHub Desktop.
Save stupeters187/b6ddc3f21edc3a0d4d392b1bf060ebe7 to your computer and use it in GitHub Desktop.
@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]
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