Skip to content

Commit a160ef6

Browse files
authored
Merge pull request #15 from nickolasteixeira/chapter3
Chapter3
2 parents 71f1ae8 + 80a39f1 commit a160ef6

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/python3
2+
'''Queue via Stacks: Implement a MyQueue class
3+
which implements a queue using two stacks.'''
4+
import unittest
5+
6+
7+
class Stack:
8+
def __init__(self):
9+
self.stack = []
10+
self.min = None
11+
12+
def getMin(self):
13+
return self.min
14+
15+
def pop(self):
16+
if not self.length:
17+
print('No stack to pop')
18+
return
19+
20+
item = self.stack.pop()
21+
22+
if item == self.min:
23+
if self.length() > 0:
24+
self.findMin()
25+
else:
26+
self.min = None
27+
return item
28+
29+
def findMin(self):
30+
self.min = self.stack[0]
31+
for x in self.stack:
32+
if x < self.min:
33+
self.min = x
34+
35+
def length(self):
36+
return len(self.stack)
37+
38+
def push(self, value):
39+
if not self.min:
40+
self.min = value
41+
elif value < self.min:
42+
self.min = value
43+
44+
self.stack.append(value)
45+
46+
def peek(self):
47+
if not self.length:
48+
print('No stack to peek')
49+
return
50+
return self.stack[-1]
51+
52+
def printStack(self):
53+
if not self.length:
54+
print('No stack to peeek')
55+
return
56+
57+
print("Top of stack\n _ ")
58+
for x in self.stack[::-1]:
59+
print('|', x, '|')
60+
print('|', '_', '|')
61+
62+
63+
class MyQueue:
64+
def __init__(self):
65+
self.newStack = Stack()
66+
self.oldStack = Stack()
67+
68+
def push(self, value):
69+
self.newStack.push(value)
70+
71+
def pop(self):
72+
self.rotate()
73+
if self.oldStack.length() == 0:
74+
print('Nothing to pop in this queue')
75+
return
76+
return self.oldStack.pop()
77+
78+
def peek(self):
79+
self.rotate()
80+
if self.oldStack.length() == 0:
81+
print('Nothing to peek in this queue')
82+
return
83+
return self.oldStack.peek()
84+
85+
def rotate(self):
86+
if (self.oldStack.length() == 0):
87+
while not self.newStack.length() == 0:
88+
self.oldStack.push(self.newStack.pop())
89+
90+
def printQueue(self):
91+
self.rotate()
92+
for x in self.oldStack.stack[::-1]:
93+
print('Item in Queue -->', x)
94+
95+
for x in self.newStack.stack:
96+
print('Item in Queue -->', x)
97+
98+
99+
class Test(unittest.TestCase):
100+
@classmethod
101+
def setUpClass(cls):
102+
pass
103+
104+
@classmethod
105+
def tearDownClass(cls):
106+
pass
107+
108+
def test1(self):
109+
arr = [2, 5, 7, 10, 3, 10, -1, -100]
110+
print(arr)
111+
q = MyQueue()
112+
for x in arr:
113+
q.push(x)
114+
115+
q.printQueue()
116+
print('First in Queue --> Popped', q.pop())
117+
print('Second in Queue --> Popped', q.pop())
118+
q.printQueue()
119+
print('Let\'s take a peek -->', q.peek())
120+
121+
122+
if __name__ == '__main__':
123+
unittest.main()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/python3
2+
'''Queue via Stacks: Implement a MyQueue class
3+
which implements a queue using two stacks.'''
4+
import unittest
5+
6+
7+
class Stack:
8+
def __init__(self):
9+
self.stack = []
10+
11+
def pop(self):
12+
if self.isEmpty():
13+
print('No stack to pop')
14+
return
15+
16+
return self.stack.pop()
17+
18+
def isEmpty(self):
19+
return len(self.stack) == 0
20+
21+
def push(self, value):
22+
self.stack.append(value)
23+
24+
def peek(self):
25+
if self.isEmpty():
26+
print('No stack to peek')
27+
return
28+
return self.stack[-1]
29+
30+
def printStack(self):
31+
if self.isEmpty():
32+
print('No stack to peeek')
33+
return
34+
35+
print("Top of stack\n _ ")
36+
for x in self.stack[::-1]:
37+
print('|', x, '|')
38+
print('|', '_', '|')
39+
40+
def sort(self):
41+
r = Stack()
42+
while not self.isEmpty():
43+
temp = self.pop()
44+
print('Temp ->', temp)
45+
while not r.isEmpty() and r.peek() > temp:
46+
self.push(r.pop())
47+
r.push(temp)
48+
print('self stack', self.stack)
49+
print(r.stack, end='\n\n')
50+
51+
while not r.isEmpty():
52+
self.push(r.pop())
53+
54+
55+
class Test(unittest.TestCase):
56+
@classmethod
57+
def setUpClass(cls):
58+
pass
59+
60+
@classmethod
61+
def tearDownClass(cls):
62+
pass
63+
64+
def test1(self):
65+
arr = [2, 5, 7, 10, 3, 10, -1, -100]
66+
print(arr)
67+
q = Stack()
68+
for x in arr:
69+
q.push(x)
70+
71+
q.printStack()
72+
q.sort()
73+
q.printStack()
74+
75+
76+
if __name__ == '__main__':
77+
unittest.main()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python3
2+
'''Animal Shelter: An animal shelter, which holds only
3+
dogs and cats, operates on a strictly"first in, first
4+
out" basis. People must adopt either the "oldest"
5+
(based on arrival time) of all animals at the shelter,
6+
or they can select whether they would prefer a dog or
7+
a cat (and will receive the oldest animal of that type).
8+
They cannot select which specific animal they would
9+
like. Create the data structures to maintain this
10+
system and implement operations such as enqueue,
11+
dequeueAny, dequeueDog, and dequeueCat. You may use
12+
the built-in Linked list data structure.
13+
Hints: #22, #56, #63'''
14+
import unittest
15+
16+
17+
class ShelterQueue:
18+
def __init__(self):
19+
self.queue = []
20+
21+
def enqueue(self, animal):
22+
if len(self.queue) == 0:
23+
self.queue.append(animal)
24+
else:
25+
self.queue.insert(0, animal)
26+
27+
def printQueue(self):
28+
print('----- Beginning of Queue ------')
29+
for x in self.queue[::-1]:
30+
print(x)
31+
print('------ End of Queue ------')
32+
33+
def dequeueAnimal(self, animal):
34+
if len(self.queue) == 0:
35+
return
36+
temp = False
37+
for x in self.queue[::-1]:
38+
if animal in x:
39+
temp = x
40+
break
41+
if temp:
42+
pet = temp
43+
self.queue.remove(temp)
44+
return pet
45+
46+
def dequeueDog(self):
47+
return self.dequeueAnimal('D')
48+
49+
def dequeueCat(self):
50+
return self.dequeueAnimal('C')
51+
52+
def dequeueAny(self):
53+
if len(self.queue) == 0:
54+
return
55+
return self.queue.pop()
56+
57+
58+
class Test(unittest.TestCase):
59+
@classmethod
60+
def setUpClass(cls):
61+
q = ShelterQueue()
62+
animals = ['D1', 'D2', 'C1', 'D3', 'C2', 'C3', 'C4']
63+
for x in animals:
64+
q.enqueue(x)
65+
66+
q.printQueue()
67+
print('{} got adopted!'.format(q.dequeueDog())) # D1
68+
print('{} got adopted!'.format(q.dequeueCat())) # C1
69+
q.printQueue()
70+
71+
print('{} got adopted!'.format(q.dequeueAny())) # D2
72+
print('{} got adopted!'.format(q.dequeueCat())) # C2
73+
print('{} got adopted!'.format(q.dequeueAny())) # D3
74+
print('{} got adopted!'.format(q.dequeueDog())) # No Dogs
75+
print('{} got adopted!'.format(q.dequeueAny())) # C3
76+
print('{} got adopted!'.format(q.dequeueCat())) # C4
77+
print('{} got adopted!'.format(q.dequeueCat())) # No Cats
78+
79+
@classmethod
80+
def tearDownClass(cls):
81+
pass
82+
83+
def test1(self):
84+
pass
85+
86+
87+
if __name__ == '__main__':
88+
unittest.main()

0 commit comments

Comments
 (0)