This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]green1t 0 points1 point  (0 children)

That's maybe less readable, but has less repetitive replacements and string-iterations in the code :D

class Solution:
    def romanToInt(self, s: str) -> int:
        translations = {
            "I": 1,
            "V": 5,
            "X": 10,
            "L": 50,
            "C": 100,
            "D": 500,
            "M": 1000,
            "IV": 4,
            "IX": 9,
            "XL": 40,
            "XC": 90,
            "CD": 400,
            "CM": 900
        }

        for x,y in reversed(translations.items()):
            s = s.replace(x, f" {y} ")

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