Skip to content

Instantly share code, notes, and snippets.

@ksin
Created July 30, 2015 02:59
Show Gist options
  • Save ksin/70dc816c3dc2d6cb3ce2 to your computer and use it in GitHub Desktop.
Save ksin/70dc816c3dc2d6cb3ce2 to your computer and use it in GitHub Desktop.
def to_roman(num)
results = ""
if num % 5 == 4
if num > 5
results = "IX"
else
results = "IV"
end
elsif num == 5
results = "V"
elsif num == 10
results = "X"
else
mod = num % 5
if num > 5
results = "V" + ("I" * mod)
else
results = "I" * mod
end
end
results
end
# Driver code... this should print out trues.
puts to_roman(1) == "I"
puts to_roman(3) == "III"
puts to_roman(6) == "VI"
puts to_roman(9) == "IX"
# TODO: what other cases could you add to ensure your to_roman method is working?
puts to_roman(14) == "XIV"
puts to_roman(19) == "XIX"
puts to_roman(20) == "XX"
# write tests up to 9998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment