nums = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} def calc(rom): total = 0 for i, s in enumerate(rom): c = nums[s] if i != len(rom) - 1 and nums[rom[i + 1]] > c: total -= c else: total += c return total def compare_roman(a, b): return calc(a) > calc(b) or -(calc(a) < calc(b))
- nums = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
- def calc(rom):
- total = 0
- for i, s in enumerate(rom):
- c = nums[s]
- if i != len(rom) - 1 and nums[rom[i + 1]] > c:
- total -= c
- else:
- total += c
- return total
- def compare_roman(a, b):
a = calc(a)b = calc(b)return a > b or -(a < b)- return calc(a) > calc(b) or -(calc(a) < calc(b))