roman to numeral

🏠

Leetcode link. Beats 100% in memory, and 91% in speed.

1from functools import reduce
2from operator import add, sub
3
4S = dict(I=1, V=5, X=10, L=50, C=100, D=500, M=1000)
5
6class Solution:
7    def romanToInt(self, s: str) -> int:
8        r = lambda a, b: (add, sub)[S[s[b+1]] > S[s[b]]](a, S[s[b]])
9        return reduce(r, reversed(range(len(s)-1)), S[s[-1]])

Discuss.