4 ms·
It's pretty horrible, but this adds support for parentheses inside the `full_eval` function: from itertools import takewhile, dropwhile def split_paren(ex
by ogogmad 6y ago
It's pretty horrible, but this adds support for parentheses inside the `full_eval` function:
from itertools import takewhile, dropwhile
def split_paren(expr):
left = ''.join(takewhile(lambda c: c != '(', expr))
right = ''.join(takewhile(lambda c: c != ')', expr[::-1]))[::-1]
inside = ''.join(dropwhile(lambda c: c != ')', ''.join(dropwhile(lambda c: c != '(', expr))[::-1]))[::-1][1:-1]
return left, inside, right
def full_eval(expr):
if '(' in expr:
left, inside, right = split_paren(expr)
return full_eval(left + str(full_eval(inside)) + right)
else:
return calc(expr)
- wryun 6y agoNeat! Well, sort of. Sometimes, certain things would look nicer not written in Python...