Converting Roman Numerals to integers in python

前端 未结 17 1665
挽巷
挽巷 2020-12-03 03:56

This is now my current code after what user2486 said.

def romanMap():
    map=((\"M\",  1000),(\"CM\", 900),(\"D\",  500),(\"CD\", 400),(\"C\",  100),(\"XC\"         


        
17条回答
  •  孤街浪徒
    2020-12-03 04:15

    def romanToInt(self, s: str) -> int:
        roman_dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        int_equ = 0
    
        for i in range(len(s)):
            if i > 0 and roman_dict[s[i]] > roman_dict[s[i-1]]:
                int_equ += roman_dict[s[i]] - 2*roman_dict[s[i-1]]
            else:
                int_equ += roman_dict[s[i]]
    
        return int_equ
    

提交回复
热议问题