Created
November 10, 2020 09:30
-
-
Save HakimCassimallyBBC/bcedf121f1f57e90f7bac23261df0b2c to your computer and use it in GitHub Desktop.
Rather "un-pythonic" FP-ish (but with explicit array access) snippet, discovered in an algorithm book
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def roman_to_integer(s: str) -> int: | |
T = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} | |
return functools.reduce( | |
lambda val, i: val + (-T[s[i]] if T[s[i]] < T[s[i+1]] else T[s[i]]), | |
reversed(range(len(s)-1)), T[s[-1]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment