Dashboard Temp Share Shortlinks Frames API

HTMLify

roman_to_int.py
Views: 430 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def roman_to_int(s):
    romans = {'I': 1,
    'V': 5,'X': 10,
    'L': 50, 'C': 100,
    'D': 500, 'M': 1000}
    result = 0
    prev_value = 0
    for c in s[::-1]:
        value = romans[c]
        if value < prev_value:
            result -= value
        else:
            result += value
        prev_value = value
    return result