Created
November 26, 2015 11:58
-
-
Save lanfon72/20ffc9176b998eca68dd to your computer and use it in GitHub Desktop.
simple python
This file contains hidden or 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
| 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