class Solution: def romanToInt(self, s): values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000} result = 0 for i in range(len(s)): if i + 1 < len(s) and values[s[i]] < values[s[i + 1]] : # if current item is not the last item on the string # and current item's value is smaller than next item's value result = result - values[s[i]] # then subtract current item's value from result else: result = result + values[s[i]] # otherwise add current item's value to result return result Task = Solution() print(Task.romanToInt("III")) print(Task.romanToInt("LVIII")) print(Task.romanToInt("MCMXCIV"))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter