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
@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