Last active
December 19, 2015 20:19
-
-
Save kmazanec/6012396 to your computer and use it in GitHub Desktop.
Modern Roman Numeral generator - am I missing a way to make this simpler? I know I could use a case statement and tighten things up that way, but logically, am I missing something?
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 roman input | |
if input < 0 || input >= 10000 | |
'Out of bounds' | |
elsif input >= 1000 && input < 10000 | |
'M' * (input / 1000) + roman(input % 1000) | |
elsif input >=900 && input < 1000 | |
'CM' + roman(input-900) | |
elsif input >= 500 && input < 900 | |
'D' + roman(input-500) | |
elsif input >= 400 && input < 500 | |
'CD' + roman(input-400) | |
elsif input >= 100 && input < 400 | |
'C' * (input / 100) + roman(input % 100) | |
elsif input >= 90 && input < 100 | |
'XC' + roman(input-90) | |
elsif input >= 50 && input < 90 | |
'L' + roman(input-50) | |
elsif input >= 40 && input < 50 | |
'XL' + roman(input-40) | |
elsif input >= 10 && input < 40 | |
'X' * (input/10) + roman(input % 10) | |
elsif input == 9 | |
'IX' | |
elsif input >= 5 && input < 10 | |
'V' + roman(input-5) | |
elsif input == 4 | |
'IV' | |
elsif input >= 1 && input < 4 | |
'I' * input | |
else | |
'' | |
end | |
end | |
my_entry = "" | |
while my_entry != "end" | |
print "Integer to see as Roman numeral ('end' to quit): " | |
my_entry = gets.chomp | |
break if my_entry == "end" | |
puts "#{my_entry} in roman numerals is: #{roman(my_entry.to_i)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment