Converting Roman Numerals to integers in python

前端 未结 17 1598
挽巷
挽巷 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:06

    roman_conver=[  (1,'I'),
                    (5,'V'),
                    (10,'X'),
                    (50,'L'),
                    (100,'C'),
                    (500,'D'),
                    (1000,'M'),
                        ]
    def romantonumeral(roman):
        tot = 0
        for i in range(0,len(roman)):
            for each in roman_conver:
                if roman[i]==each[1]:
                    if each[0]>tot:
                        tot = each[0] - tot
                    else:
                        tot = tot + each[0]
        return tot
    

提交回复
热议问题