-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevalPostFix.py
44 lines (41 loc) · 898 Bytes
/
evalPostFix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#for prefix expression evalution : same as postfix
#just reverse and use the same : only in / and - use a and b the other way that not a-b but b-a
def evalPost(postfix):
res=[]
op=[]
for i in postfix:
if i.isdigit():
print "entering " + str(i)
res.append(i)
else:
print "entering operator" + str(i)
op.append(i)
print "len of op" + str(len(op))
if len(op) > 0:
print "popping"
b=int(res.pop())
a=int(res.pop())
print a,b
if i=="*": c=int(a*b)
if i=="+": c=int(a+b)
if i=="-": c=int(a-b)
print c
res.append(c)
return res,op
def evalPre(prefix):
prefixList=[]
for i in prefix:
prefixList.append(i)
prefixList.reverse()
print prefixList
return evalPost(prefixList)
#https://www.youtube.com/watch?v=MeRb_1bddWg
postfix="23*52*+"
prefix="-+*23*549"
print evalPost(postfix)
#print evalPre(prefix)
#5 2
#10
#3 10
#13
#['2', 13]