- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalPostFix.py
More file actions
Latest commit
44 lines (41 loc) · 898 Bytes
/
Copy pathevalPostFix.py
File metadata and controls
44 lines (41 loc) · 898 Bytes
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
defevalPost(postfix):
res=[]
op=[]
foriinpostfix:
ifi.isdigit():
print"entering "+str(i)
res.append(i)
else:
print"entering operator"+str(i)
op.append(i)
print"len of op"+str(len(op))
iflen(op) >0:
print"popping"
b=int(res.pop())
a=int(res.pop())
printa,b
ifi=="*": c=int(a*b)
ifi=="+": c=int(a+b)
ifi=="-": c=int(a-b)
printc
res.append(c)
returnres,op
defevalPre(prefix):
prefixList=[]
foriinprefix:
prefixList.append(i)
prefixList.reverse()
printprefixList
returnevalPost(prefixList)
#https://www.youtube.com/watch?v=MeRb_1bddWg
postfix="23*52*+"
prefix="-+*23*549"
printevalPost(postfix)
#print evalPre(prefix)
#5 2
#10
#3 10
#13
#['2', 13]