@@ -7,6 +7,18 @@ def __init__(self):
7
7
self .length = 0
8
8
9
9
def __str__ (self ):
10
+ """
11
+ >>> q = Queue()
12
+ >>> q.put(3)
13
+ >>> q.put(5)
14
+ >>> q.put(7)
15
+ >>> print(q)
16
+ <3, 5, 7>
17
+
18
+ >>> q = Queue()
19
+ >>> print(q)
20
+ <>
21
+ """
10
22
printed = "<" + str (self .stack )[1 :- 1 ] + ">"
11
23
return printed
12
24
@@ -15,6 +27,12 @@ def __str__(self):
15
27
item to enqueue"""
16
28
17
29
def put (self , item ):
30
+ """
31
+ >>> q = Queue()
32
+ >>> q.put(3)
33
+ >>> assert q.stack == [3]
34
+ >>> assert q.length == 1
35
+ """
18
36
self .stack .append (item )
19
37
self .length = self .length + 1
20
38
@@ -24,6 +42,37 @@ def put(self, item):
24
42
item that was dequeued"""
25
43
26
44
def get (self ):
45
+ """
46
+ Testing get() and inner call to rotate()
47
+
48
+ >>> q = Queue()
49
+ >>> q.put(3)
50
+ >>> q.put(5)
51
+ >>> q.put(7)
52
+ >>> n = q.get()
53
+ >>> assert n == 3
54
+ >>> assert q.stack == [5,7]
55
+
56
+ >>> q2 = Queue()
57
+ >>> q2.put(3)
58
+ >>> n = q2.get()
59
+ >>> n = q2.get()
60
+ Traceback (most recent call last):
61
+ ...
62
+ IndexError: The queue is empty
63
+
64
+ >>> q3 = Queue()
65
+ >>> n = q3.get()
66
+ Traceback (most recent call last):
67
+ ...
68
+ IndexError: The queue is empty
69
+
70
+ >>> q4 = Queue()
71
+ >>> q4.put(3)
72
+ >>> n = q4.get()
73
+ >>> assert n == 3
74
+ """
75
+
27
76
self .rotate (1 )
28
77
dequeued = self .stack [self .length - 1 ]
29
78
self .stack = self .stack [:- 1 ]
@@ -33,10 +82,22 @@ def get(self):
33
82
34
83
"""Rotates the queue {@code rotation} times
35
84
@param rotation
85
+ @requirement: |self.length| > 0
36
86
number of times to rotate queue"""
37
87
38
88
def rotate (self , rotation ):
89
+ """
90
+ >>> q = Queue()
91
+ >>> q.put(3)
92
+ >>> q.put(5)
93
+ >>> q.put(7)
94
+ >>> q.rotate(2)
95
+ >>> assert q.stack == [7,3,5]
96
+ """
97
+
39
98
for i in range (rotation ):
99
+ if self .length == 0 :
100
+ raise IndexError ("The queue is empty" )
40
101
temp = self .stack [0 ]
41
102
self .stack = self .stack [1 :]
42
103
self .put (temp )
@@ -46,6 +107,18 @@ def rotate(self, rotation):
46
107
@return item at front of self.stack"""
47
108
48
109
def front (self ):
110
+ """
111
+ >>> q = Queue()
112
+ >>> q.put(3)
113
+ >>> n = q.front()
114
+ >>> assert n == 3
115
+
116
+ >>> q2 = Queue()
117
+ >>> n = q2.front()
118
+ Traceback (most recent call last):
119
+ ...
120
+ IndexError: The queue is empty
121
+ """
49
122
front = self .get ()
50
123
self .put (front )
51
124
self .rotate (self .length - 1 )
@@ -54,4 +127,15 @@ def front(self):
54
127
"""Returns the length of this.stack"""
55
128
56
129
def size (self ):
130
+ """
131
+ >>> q = Queue()
132
+ >>> q.size()
133
+ 0
134
+ """
57
135
return self .length
136
+
137
+
138
+ if __name__ == "__main__" :
139
+ import doctest
140
+
141
+ doctest .testmod ()
0 commit comments