forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_q.py
executable file
·161 lines (136 loc) · 4.22 KB
/
stack_q.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
#!/usr/bin/env python
# In python, we use list function append() as push
# pop() as pop
# For Queue, use from collections import deque as queue
# Use append() as push, use popleft() as pop
class my_queue:
def __init__(self):
self.stack_a = []
self.stack_b = []
self.storing_a = True
def push(self, data):
if storing_a:
self.stack_a.append(data)
else:
self.stack_b.append(data)
def pop(self):
# Data is in A, so first push data to B and pop the last one in queue
if storing_a:
while len(a)>0:
b.append(a.pop())
storing_a = False
return b.pop()
else:
while len(b)>0:
a.append(b.pop())
storing_a = True
return a.pop()
class new_stack:
def __init__(self):
self.stack = []
self.min = None
self.min_list = []
def pop(self):
self.min_list.pop()
return self.stack.pop()
def push(self, data):
if data < self.min_list[len(min_list)-1]:
self.min_list.append(data)
else:
self.min_list.append(self.min_list[len(min_list)-1])
self.stack.append(data)
def get_min(self):
return self.min_list.pop()
def sort_stack1(stack):
temp_stack = [stack.pop(),]
temp_var = None
while len(stack) > 0:
temp_var = stack.pop()
while len(temp_stack)>0 and temp_stack[len(temp_stack)-1] > temp_var:
stack.append(temp_stack.pop())
temp_stack.append(temp_var)
return temp_stack
def sort_stack(stack):
temp = [stack.pop(),]
while len(stack) > 0:
print '*'*10
print 'in stack',stack
print 'in temp',temp
# check if should put equal here
if stack[len(stack)-1] >= temp[len(temp)-1]:
print 'pushing to temp'
temp.append(stack.pop())
else:
var = stack.pop()
while len(temp) > 0:
print 'VAR',var
if var is None or var < temp[len(temp)-1]:
print 'temp peek', temp[len(temp)-1]
stack.append(temp.pop())
print 'temp_stack', temp
else:
print 'appending var'
stack.append(var)
var = None
# VERRRRRRRRRRY IMPORTANT, don't forget to push the last var
if var is not None:
temp = [var,]
else:
temp = [stack.pop(),]
return temp
class Animal:
def __init__(self, name):
self.name = name
def set_order(self, order):
self.order = order
def get_order(self, order):
return self.order
def is_older_than(Animal):
if self.order < Animal.get_order():
return True
else:
return False
class Cat(Animal):
def __init__(self, name):
self.super()
class Dog(Animal):
def __init__(self, name)
from collection import deque
class AnimalQueue:
def __init__(self):
self.cat_queue = deque()
self.dog_queue = deque()
self.order = 0
def add_animal(self, animal):
if isinstance(animal, Cat):
cat_queue.append(animal)
else:
dog_queue.append(animal)
animal.set_order(order)
self.order += 1
def deq_dog(self):
if len(self.dog_queue) == 0:
return 'No dog left'
return self.dog_queue.popleft()
def deq_cat(self):
if len(self.cat_queue) == 0:
return 'No cat left'
return self.cat_queue.popleft()
def deq_any(self):
if len(self.cat_queue) == 0 and len(self.dog_queue) ==0:
return 'No Animal Left!'
elif len(self.cat_queue) == 0:
self.deq_dog()
elif len(self.dog_queue) == 0:
self.deq_cat()
# Great, you have the chance to get either a cat or dog
else:
if cat_queue[0].is_older_than(dog_queue[0]):
self.deq_cat()
else:
self.deq_dog()
# Missing 3.1 3.3 3.4
if __name__ == '__main__':
stack = [8,4,7,9,2,1,6,5,10]
new_stack = sort_stack(stack)
print new_stack