A
adamw
Hi
I'm new to Ruby and programming and currently trying to write a program
converting Roman numerals to integers. I've done the following so far
(number validation check still not good) and stuck on the step when I
need to delete a specified string from the beginning (left side) of
some string and keep the rest. I've tried many String class methods but
none of them seems to meet my needs.
def roman_to_integer number
number = number.to_s
result = 0
roman = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V',
'IV', 'I' ]
arab = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
if (number.capitalize).include?('M' || 'D' || 'C' || 'L' || 'X' ||
'V' || 'I')
n = 0
while n < 13
while number.include?(roman[n])
number[n].delete(roman[n]) <- THIS IS THE LINE
result = result + arab[n]
end
n += 1
end
print 'It equals: ' + result.to_s
else
puts 'Please enter a valid Roman numeral'
end
end
roman_to_integer 'MCmLxxVI'
Please help
Cheers
Adam
I'm new to Ruby and programming and currently trying to write a program
converting Roman numerals to integers. I've done the following so far
(number validation check still not good) and stuck on the step when I
need to delete a specified string from the beginning (left side) of
some string and keep the rest. I've tried many String class methods but
none of them seems to meet my needs.
def roman_to_integer number
number = number.to_s
result = 0
roman = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V',
'IV', 'I' ]
arab = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
if (number.capitalize).include?('M' || 'D' || 'C' || 'L' || 'X' ||
'V' || 'I')
n = 0
while n < 13
while number.include?(roman[n])
number[n].delete(roman[n]) <- THIS IS THE LINE
result = result + arab[n]
end
n += 1
end
print 'It equals: ' + result.to_s
else
puts 'Please enter a valid Roman numeral'
end
end
roman_to_integer 'MCmLxxVI'
Please help
Cheers
Adam