Skip to content

Instantly share code, notes, and snippets.

@lakinwecker
Created March 4, 2017 00:40
Show Gist options
  • Save lakinwecker/717d533b2817842de61911c5adbd50c8 to your computer and use it in GitHub Desktop.
Save lakinwecker/717d533b2817842de61911c5adbd50c8 to your computer and use it in GitHub Desktop.
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