-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressExecute.py
More file actions
367 lines (308 loc) · 7.45 KB
/
Copy pathexpressExecute.py
File metadata and controls
367 lines (308 loc) · 7.45 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#execute in PYTHON shell
# exec(open('pythonplay.py').read())
class OpNode_Final:
def __init__(self, val):
self.value = val
self.left = None
self.right = None
self.level = -10
self.adjustLevel()
def adjustLevel(self):
if (self.value == "+" or self.value == "-"):
self.level = 10
elif (self.value == "*" or self.value == "/"):
self.level = 20
elif (self.value == "("):
self.level = 30
elif (self.value == ")"):
self.level = 40
elif (isinstance(self.value, int)):
self.level = 0
else:
print " Undefined object, please check"
def compute(self):
#compute the expression
if isinstance(self.value, str):
if (self.value == "+"):
res = self.left.compute() + self.right.compute()
elif (self.value == "-"):
res = (self.left.compute() - self.right.compute())
elif (self.value == "*"):
res = (self.left.compute() * self.right.compute())
elif (self.value == "/"):
res = (self.left.compute() / self.right.compute())
else:
res = self.value
#print "compute", res
return res
def setLeft(self, lft):
self.left = lft
def setRight(self, rg):
self.right = rg
def show(self):
if (self.left != None):
self.left.show()
print " ", self.value
if (self.right != None):
self.right.show()
def showWidthFirst(self):
print " ", self.value
if (self.left != None):
self.left.showWidthFirst()
if (self.right != None):
self.right.showWidthFirst()
def retrieveDataWidthFirst(self, dst, level, nodeId = [0]):
if level == 0:
print " retrieveData width first"
if len(dst) < level + 1:
dst.append([])
dst[level].append("L:"+str(level))
dst[level].append(("N:"+str(nodeId[0]), self.value))
nodeId[0] += 1
if (self.left != None):
self.left.retrieveDataWidthFirst(dst, level+1, nodeId)
if (self.right != None):
self.right.retrieveDataWidthFirst(dst, level+1, nodeId)
def doPopRightmost(stk):
idx = findLastLeftParenthesisPos(stk)
if idx >= 0:
# print " last ( is in", idx
cnt = len(stk) - idx -1
else:
idx = 0;
cnt = len(stk)
#print " right-most, len", len(stk), " ( pos:", idx, "proc cnt:",cnt
if cnt == 1:
return True;
elif cnt == 3:
# process "a+b" style
# watch and wait
#print " no more process, wait"
return True
elif len(stk) == 5:
# a+b*c, process
#print "process 1,", stk[-2].level , stk[-4].level
if (stk[-2].level > stk[-4].level):
#print " process: a+b*c"
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
return True
else:
#print "bad right-most process, check len", len(stk)
return False
def findLastLeftParenthesisPos(stk):
idx = len(stk)
if idx == 0:
return -1
idx -= 1
while (idx >= 0):
if (stk[idx].value == "("):
return idx;
idx -= 1
return -1
def doPop_Parenthesis(stk):
idx = findLastLeftParenthesisPos(stk)
if idx >= 0:
#print " last ( is in", idx
pass
else:
idx = 0;
cnt = len(stk)
#print " left ( is not found, ERROR"
return
p = 0
#pop 3 or 5
if len(stk) <3:
#print "bad stack, check len", len(stk)
return
if (stk[-3].value == "("):
# process "(a)" style
#print " pop style (a)"
stk.pop()
op = stk.pop()
stk.pop()
stk.append(op)
p = 1
elif (stk[-7].value == "("):
# "(a+b*c)"
stk.pop()
p3 = stk.pop()
op2 = stk.pop()
p2 = stk.pop()
op1 = stk.pop()
p1 = stk.pop()
stk.pop()
if (op2.level > op1.level):
#print " pop style (a+b*c)"
# "a+b*c"
op2.setLeft(p2)
op2.setRight(p3)
op1.setLeft(p1)
op1.setRight(op2)
stk.append(op1)
else:
# "a*b+c" or "a+b-c" or...
#print " pop style a*b+c or a+b-c"
op1.setLeft(p1)
op1.setRight(p2)
op2.setLeft(op1)
op2.setRight(p3)
stk.append(op2)
p = 1
elif (stk[-5].value == "("):
# "(a+b)"
#print " pop style (a+b)"
stk.pop()
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.pop()
#push back result
stk.append(op)
p = 1
else:
print " ERROR: bad parenthesis"
if p == 1:
# processed ")", check again and process
doPopRightmost(stk)
if p == 0:
print " bad stack, check len", len(stk)
return
def checkStack(stk, t):
if (len(stk) == 0):
return
idx = findLastLeftParenthesisPos(stk)
if idx >= 0:
#print " last ( is in", idx
cnt = len(stk) - idx -1
else:
idx = 0;
cnt = len(stk)
if cnt == 3:
# a+b, a*b...
if t.level == 10:
# if it is +/-, just do a pop of left
#print " pop a lower oper"
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
elif t.level == 20:
# 3*4* (same level)
if stk[-2].level == 20:
#print " pop same level"
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
else:
#print " wait next"
pass
elif cnt == 5:
# a+b*c is possible
# a*b+c is not possible
# no matter, must pop the right most
#print " pop the right most"
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
def popLast(stk):
#print "pop last", len(stk)
while (len(stk) >= 3):
if len(stk) == 3:
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
elif len(stk) == 5:
# a+b*c is possible
# a*b+c is not possible
# no matter, must pop the right most
p2 = stk.pop()
op = stk.pop()
p1 = stk.pop()
op.setLeft(p1)
op.setRight(p2)
stk.append(op)
#print " pop the right most, after, len", len(stk)
def parseFullExpressLast(expr):
stk = []
x = 0
print expr
pos = 0
for x in expr:
#print "len", len(stk), " :", x
# put into stack
t = OpNode_Final(x)
if t.level == 40:
# ")"
stk.append(t)
#print "push :", x
doPop_Parenthesis(stk)
elif t.level == 30:
# "("
stk.append(t)
#print "push :", x
elif t.level == 0:
stk.append(t)
#print "push int:", x
elif t.level == 10 or t.level == 20:
# put into stack
# check whether poper
checkStack(stk, t)
stk.append(t)
#print "push:", x
else:
print "bad input, check", x
#let pos move one
pos +=1
#check last
popLast(stk)
return stk.pop()
#use a string to generate a list of operation
def generateList(ss):
pos = 0
x = 0
out = []
print ss
ss=ss.replace(" ","")
print ss
while (x < len(ss)):
if not (ss[x] >='0' and ss[x] <='9'):
if (pos < x):
out.append(int(ss[pos: x])) #convert them to int
pos = x+1;
out.append(ss[x])
pos = x+1
x += 1
# the last
if (pos < x):
out.append(int(ss[pos: x]))
return out
res = parseFullExpressLast(generateList("3 +(5*(4/2)*3-2)+(1+2)*(6-4)+( 5)"))
if (res is None):
print "bad result"
else:
print " >>>> depth first"
res.show()
print " >>>> width first"
val = []
res.retrieveDataWidthFirst(val, 0)
for x in val:
print x
print "the last result is", res.compute()