the art of
Algorithm
Notes on Analysis and Design



Reverse Polish Notation

Program for implementing reverse polish notation or infix to postfix conversion in stack in python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def rpn(s):
    var_list=[]
    sym_list=[]
    for c in s:
        if c >='a' and c <='z':
            var_list.append(c)
        elif c in ['^', '*', '/', '+', '-']:
            sym_list.append(c)
        elif c == ')':
            x=var_list.pop()
            y=var_list.pop()
            z=sym_list.pop()
            var_list,append(x+y+z)
    return var_list[0]
s=raw_input()
print rpn(s)