Skip to content

Instantly share code, notes, and snippets.

@tinomen
Created March 27, 2013 02:14
Show Gist options
  • Save tinomen/5251012 to your computer and use it in GitHub Desktop.
Save tinomen/5251012 to your computer and use it in GitHub Desktop.
2013-03 slc.rb problem 2
Convert a number between 1 and 4999 (integer only) to the roman numeral representation.
make a command line program that takes 1 argument as a number and outputs the roman numerals.
An online converter is available at http://www.onlineconversion.com/roman_numerals_advanced.htm
@justinlyman
Copy link

LETTERS = [ ["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 romanize number
  num = number
  count = 0
  LETTERS.map{|l,v| count, num = num.to_i.divmod v; l*count}.join ''
end
puts 'Input number to Convert to Roman Numeral:'
input = gets
puts romanize(input)

@cmaxw
Copy link

cmaxw commented Mar 27, 2013

puts "What number do you want converted?"
number = gets.chomp.to_i

answer = "I" * number
answer.gsub!("I" * 1000, "M")
answer.gsub!("I" * 900, "CM")
answer.gsub!("I" * 500, "D")
answer.gsub!("I" * 400, "CD")
answer.gsub!("I" * 100, "C")
answer.gsub!("I" * 90, "XC")
answer.gsub!("I" * 50, "L")
answer.gsub!("I" * 40, "XL")
answer.gsub!("I" * 10, "X")
answer.gsub!("I" * 9, "IX")
answer.gsub!("I" * 5, "V")
answer.gsub!("I" * 4, "IV")


puts answer

@rickarubio
Copy link

not working, this is as far as I got

convert a number between 1 to 99 to the roman numberal representation

You must separate ones, tens, hundreds, and thousands as separate items.

You would not put more than one smaller number in front of a larger number to subtract.

A smaller number in front of a larger number means subtraction, all else means addition.

def convertTens(anInteger)
# as long as I have something on the left side of the decimal, I must have a tens position
if ( (anInteger / 10) > 0)
tensInteger = anInteger / 10
case tensInteger
when 1
tensNumeral = "X"
when 2
tensNumeral = "XX"
when 3
tensNumeral = "XXX"
when 4
tensNumeral = "XL"
when 5
tensNumeral = "L"
when 6
tensNumeral = "LX"
when 7
tensNumeral = "LXX"
when 8
tensNumeral = "LXXX"
when 9
tensNumeral = "XC"
end
end
return tensNumeral
end

def convertOnes(anInteger)
if ( (anInteger % 0.1) > 0)
puts "testing"
#problem is here with integer to floating point conversion
puts (anInteger % 0.1)
onesInteger = anInteger % 0.1
case onesInteger
when 1
onesNumeral = "I"
when 2
onesNumeral = "II"
when 3
onesNumeral = "III"
when 4
onesNumeral = "IV"
when 5
onesNumeral = "V"
when 6
onesNumeral = "VI"
when 7
onesNumeral = "VII"
when 8
onesNumeral = "VIII"
when 9
onesNumeral = "IX"
end
end
return onesNumeral
end

result = convertTens(99).to_s + convertOnes(99).to_s
puts result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment