-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap06.py
349 lines (291 loc) · 6.88 KB
/
chap06.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
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
# R1
3,8,2,1,6,7,4,9
# R2
18
# R3
def transfer(S,T):
for i in range(len(S)):
temp = S.pop()
T.append(temp)
return S,T
# R4
def rec(S):
if len(S) == 1:
S.pop()
else:
S.pop()
rec(S)
return S
# R5
def reverser(Seq, Sta = []):
for i in range(len(Seq)):
Sta.append(Seq.pop(0))
for i in range(len(Sta)):
Seq.append(Sta.pop())
return Seq
# R6
"""implement code instead"""
def match(Seq, Stack=[]):
left = "{[("
right = "}])"
for i in Seq:
if i in left:
Stack.append(i)
elif i in right:
temp = right.index(i)
top = left.index(Stack.pop())
if temp == top:
continue
else:
print('error')
break
else:
continue
if Stack:
print("error again")
# R7
5,3,2,8,9,1,7,6
# R8
22
# R9
Non-Coding
# R10
Non-Coding
# R11
from collections import deque
queue = deque([1,2,3])
# R12
9,5,9,4,7,4,8
# R13
def switch(D,Q):
for i in range(len(D)):
Q.append(D.popleft())
# R14
"""Same as R13"""
# C15
x = stack.pop()
x = max(x, stack.pop())
# C16
def __init__(self, maxlen=None):
self._data = []
self._maxlen = maxlen
def push(self, e):
if self._maxlen <= len(self._data):
raise Full('full')
else:
self._data.append(e)
# C17
def __init__(self, maxlen=None):
self._data = [] if maxlen == None else [None]*maxlen
self._maxlen = maxlen
# C18
def transfer(S,T,U):
while S:
T.append(S.pop())
while T:
U.append(T.pop())
while U:
S.append(U.pop())
return S
# C19
tag = raw[j+1:k]
space = tag.find(' ')
if space:
actual_tag = tag[:space]
else:
actual_tag = tag
# C20
Johnson–Trotter algorithm
# C21
Non-Coding
# C22
Non-Coding
"""Implement function instead"""
def postfix(string, stack=[]):
for i in string:
if i is '*':
stack.append(stack.pop()*stack.pop())
elif i is '+':
stack.append(stack.pop()+stack.pop())
elif i is '-':
temp = stack.pop()
stack.append(stack.pop()-temp)
elif i is '/':
temp = stack.pop()
stack.append(stack.pop()/temp)
else:
stack.append(int(i))
print(stack)
return stack
# C23
Non-Coding
# C24
Non-Coding
"""implement instead"""
from collections import deque
class Stack:
def __init__(self):
self._stack = deque([])
self._storage = 0
def push(self,e):
self._stack.append(e)
self._storage += 1
def pop(self):
temp = []
while self._storage - 1 > 0:
temp.append(self._stack.popleft())
self._storage -= 1
value = self._stack.popleft()
self._stack.extend(temp)
temp = []
self._storage = len(self._stack)
return value
def top(self):
return self._stack[-1]
# C25
Non-Coding
# C26
Non-Coding
"""implement instead"""
class Deque:
def __init__(self):
self.stack1 = []
self.stack2 = []
def add_first(self, e):
while len(self.stack1) > 0:
self.stack2.append(self.stack1.pop())
self.stack1.append(e)
while len(self.stack2) > 0:
self.stack1.append(self.stack2.pop())
def add_last(self,e):
self.stack1.append(e)
def delete_first(self):
while len(self.stack1) > 1:
self.stack2.append(self.stack1.pop())
value = self.stack1.pop()
while len(self.stack2) > 0:
self.stack1.append(self.stack2.pop())
return value
def delete_last(self):
return self.stack1.pop()
def first(self):
return self.stack1[0]
def last(self):
return self.stack1[-1]
def is_empty(self):
return len(self.stack1) == 0
def __len__(self):
return len(self.stack1)
# C27
Non-Coding
"""implement instead"""
from collections import deque
def check(S,X,found = False):
Q = deque([])
while S:
temp = S.pop()
if temp == X:
found = True
Q.append(temp)
while Q:
S.append(Q.popleft())
while S:
Q.append(S.pop())
while Q:
S.append(Q.popleft())
return found, S
# C28
Same C16
# C29
def rotate(self):
temp = self._data[self._front]
self._data[self._front] = None
self._front = (self._front + 1)%len(self._data)
avail = (self._front + self.size-1)%len(self._data)
self._data[avail] = temp
# C30
Non-Coding
# C31
Non-Coding
# P32
class Deque:
def __init__(self, maxlen=None):
self._seq = []
self._size = 0
self._maxlen = maxlen
def add_first(self,e):
if self._maxlen == None:
temp = self._seq
self._seq = []
self._seq.append(e)
self._seq.extend(temp)
self._size += 1
else:
if self._size == self._maxlen:
temp = self._seq
self._seq = []
self._seq.append(e)
self._seq.extend(temp[:self._size-1])
else:
temp = self._seq
self._seq = []
self._seq.append(e)
self._seq.extend(temp)
self._size += 1
# P33
Same 32
# P34
Same 22
# P35
class LeakyStack:
def __init__(self, maxlen):
self._seq = [None]*maxlen
self._maxlen = maxlen
self._front = 0
self._size = 0
def push(self,e):
if self._size != self._maxlen:
avail = self._front % self._maxlen
self._seq[avail] = e
self._size += 1
self._front += 1
else:
avail = self._front % self._maxlen
self._seq[avail] = e
self._front += 1
def pop(self):
self._front -=1
a = self._seq[self._front % self._maxlen]
self._seq[self._front % self._maxlen] = None
self._size -=1
return a
def top(self):
return self._seq[(self._front-1) % self._maxlen]
def __len__(self):
return self._size
# P36
from collections import deque
def stocks(number):
Q,P = deque([]),deque([])
for i in range(number):
input1, input2, input3 = input("Action,Quantity,Price: ").split(',')
input2,input3 = int(input2), int(input3)
if input1 == "buy":
Q.append(input2)
P.append(input3)
else:
profit,i = 0,0
while input2 != 0:
tempQ,tempP = Q[i],P[i]
diff = input2 - tempQ
if diff < 0:
diff = input2
Q[i] -= diff
profit += (diff*(input3-tempP))
input2 -= diff
else:
input2 -= Q.popleft()
P.popleft()
profit += (tempQ*(input3-tempP))
return profit
# P37