Skip to content

Instantly share code, notes, and snippets.

@lanfon72
Created November 26, 2015 11:58
Show Gist options
  • Select an option

  • Save lanfon72/20ffc9176b998eca68dd to your computer and use it in GitHub Desktop.

Select an option

Save lanfon72/20ffc9176b998eca68dd to your computer and use it in GitHub Desktop.
simple python
def bi_exp_tree(expstr):
""" infix to postfix.
>>> bi_exp_tree("(a+(b*c))")
'abc*+'
>>> bi_exp_tree("((a+b)*(z+x))")
'ab+zx+*'
>>> bi_exp_tree("((a+t)*((b+(a+c))^(c+d)))")
'at+bac++cd+^*'
"""
result = ""
stack = []
for c in expstr:
if c in '(':
continue
if c in "+-*/^":
stack.append(c)
elif c in ')':
result += stack.pop()
else:
result += str(c)
return result
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment