๐Ÿ“ฆ schubart / LeetCode

๐Ÿ“„ lc_0013.py ยท 24 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24# noinspection PyMethodMayBeStatic,PyPep8Naming
class Solution:
    def romanToInt(self, s: str) -> int:
        substitutions = [
            ("IV", 4),
            ("IX", 9),
            ("XL", 40),
            ("XC", 90),
            ("CD", 400),
            ("CM", 900),
            ("I", 1),
            ("V", 5),
            ("X", 10),
            ("L", 50),
            ("C", 100),
            ("D", 500),
            ("M", 1000),
        ]

        for roman, arabic in substitutions:
            s = s.replace(roman, str(arabic) + ' ')

        return sum(map(int, s.split()))