-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleStackFIFOqueue.py
102 lines (80 loc) · 3.37 KB
/
DoubleStackFIFOqueue.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
# Exercise: Implement a queue with 2 stacks.
#Your queue should have an enqueue and a dequeue
#function and it should be "first in first out" (FIFO).
#Designed Algorithm:
#On enqueue we push on the ThePopStack if there is no element until there is 1 then on the second stack
#On dequeue if ThePopStack is empty we push in it all the element from the push stack which put them in LIFO order
import unittest
class DoubleStackFIFOqueue:
def __init__(self):
self.ThePopStack = []
self.ThePushStack = []
def __str__(self):
return "Pop Stack : " + str(self.ThePopStack) + "\n"\
"Push Stack : " + str(self.ThePushStack)
def enqueue(self, Obj):
if not self.ThePopStack:
self.ThePopStack.append(Obj)
else:
self.ThePushStack.append(Obj)
def dequeue(self):
if not self.ThePopStack:
raise IndexError ("Queue is Empty")
else:
TopOfThePop = self.ThePopStack.pop()
if not self.ThePopStack:
self.transferStacks()
return TopOfThePop
def transferStacks(self):
while self.ThePushStack:
self.ThePopStack.append(self.ThePushStack.pop())
class TestDoubleStackFIFOqueue(unittest.TestCase):
def setUp(self):
self.DSFQ = DoubleStackFIFOqueue()
def test_enqueue(self):
self.DSFQ.enqueue("A")
self.assertEqual(self.DSFQ.ThePopStack[0], "A",'Pop Stack expected value A')
def test_dequeue(self):
self.DSFQ.enqueue("A")
Val = self.DSFQ.dequeue()
self.assertEqual(Val, "A",'expected value A')
def test_dequeueEmpty(self):
self.assertRaises(IndexError, self.DSFQ.dequeue)
def test_ManyEnqueue(self):
self.DSFQ.enqueue("A")
self.DSFQ.enqueue("B")
self.DSFQ.enqueue("C")
self.DSFQ.enqueue("D")
self.assertEqual(self.DSFQ.ThePopStack[0], "A",'Pop Stack expected value A')
self.assertEqual(self.DSFQ.ThePushStack[0], "B",'Push Stack expected value B')
self.assertEqual(self.DSFQ.ThePushStack[1], "C",'Push Stack expected value C')
self.assertEqual(self.DSFQ.ThePushStack[2], "D",'Push Stack expected value D')
def test_ManyEnqueueDequeue(self):
self.DSFQ.enqueue("A")
self.DSFQ.enqueue("B")
self.DSFQ.enqueue("C")
self.DSFQ.enqueue("D")
TestList = []
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
self.assertEqual(TestList, ["A","B","C","D"],'expected value [A,B,C,D]')
def test_MixEnqueueDequeue(self):
TestList = []
self.DSFQ.enqueue("A")
self.DSFQ.enqueue("B")
TestList.append(self.DSFQ.dequeue())
self.DSFQ.enqueue("C")
self.DSFQ.enqueue("D")
self.DSFQ.enqueue("E")
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
self.DSFQ.enqueue("F")
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
TestList.append(self.DSFQ.dequeue())
print("Dequeue\n" + str(TestList) +"\n"+str(self.DSFQ))
self.assertEqual(TestList, ["A","B","C","D","E","F"],'expected value [A,B,C,D,E,F]')
if __name__ == '__main__':
unittest.main()