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)
- nums = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
calc = lambda rom: sum((c := nums[s]) * (1, -1)[i != len(rom) - 1 and nums[rom[i + 1]] > c] for i, s in enumerate(rom))compare_roman = lambda a, b : (a := calc(a)) > (b := calc(b)) or -(a < b)- 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)