Created
March 4, 2017 00:40
-
-
Save lakinwecker/717d533b2817842de61911c5adbd50c8 to your computer and use it in GitHub Desktop.
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 operator, re | |
lookup = { | |
'-': operator.sub, | |
'+': operator.add, | |
'*': operator.mul, | |
'/': operator.div, | |
'^': operator.pow, | |
} | |
emdas = [['\^'], ['\/\*'], ['\+\-']] | |
def lwe(expr): | |
for ops in reversed(emdas): | |
expr = re.split("([{}])".format("\\".join(ops)), expr, 1) | |
if len(expr) == 3: | |
return lookup[expr[1]](lwe(expr[0]), lwe(expr[2])) | |
expr = expr[0] | |
return float(expr) | |
print lwe("4.5*3+3*2^2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment