I am trying to solve this python problem with python3, my code looks like this.
class Solution:
def romanToInt(self, s: str) -> int:
# Define integer value to each roman
rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000}
# A list of integer values
value = list(map(rom_val.get, s))
# The subtracted new number
new = 0
# The converted integer number
integer = 0
# List to keep the checked roman
checked = []
for i, j in enumerate(value):
if j > value[i+1] or j == value[i+1]:
checked.append(j)
if j < value[i+1]:
new = value[i+1] - j
checked.append(new)
return sum(checked)
However, I am getting IndexError: list index out of range on the first if statement. Even though I know this is rather an easy type of question but there is something I don't understand about. So I have two questions: 1. of course, why I am getting this index error? how do I fix it? 2. is my way of solving this problem correct?
Thank you very much.