I'm trying to get better as a programmer and would love any feedback on my code. Especially the section in the while loop. How could I make this more readable? What would your opinion as an interviewer be of my code? I utilized python 2.7.
Challenge:
Given a Roman Numeral, convert it to an integer.
Example:
Input: "LVIII" Output: 58
Here is my Code:
def romanToInt(self, roman):
if not (roman):
return 0
LetterToValue = {'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000}
RomanCharacterized = list(roman)
End = len(RomanCharacterized)
if (End == 1):
return LetterToValue[RomanCharacterized[0]]
start = 0
next = start + 1
result = 0
while(next < End):
#Value @ Start < Value @ Immediately Following
if(LetterToValue[RomanCharacterized[start]] < LetterToValue[RomanCharacterized[next]]):
result = result + (LetterToValue[RomanCharacterized[next]] - LetterToValue[RomanCharacterized[start]])
#Value @ Start == Value @ Immediately Following
if(LetterToValue[RomanCharacterized[start]] == LetterToValue[RomanCharacterized[next]]):
result = result + (LetterToValue[RomanCharacterized[start]] + LetterToValue[RomanCharacterized[next]])
#Value @ Start > Value @ Immediately Following
if(LetterToValue[RomanCharacterized[start]] > LetterToValue[RomanCharacterized[next]]):
result = result + LetterToValue[RomanCharacterized[start]]
start = next #Needs to advance one slot
next = next + 1
else:
start = next + 1 #advances two slots
next = start + 1
if (start == (End -1)):
result = result + LetterToValue[RomanCharacterized[start]]
return result
LL
. Is this intended? \$\endgroup\$ – hoffmale Jul 10 '18 at 0:09