Skip to content

Commit 847721f

Browse files
committed
Added Doctest
1 parent 860cc55 commit 847721f

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed
Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
11
class StackUsingQueues:
2-
def __init__(self):
2+
def __init__(self) -> None:
33
"""
4-
Initialising required queues using lists data structure
4+
Initialize the stack using two queues.
5+
6+
>>> stack = StackUsingQueues()
57
"""
68
self.queue1 = []
79
self.queue2 = []
810

9-
def push(self,item):
10-
while len(self.queue1)!= 0:
11-
"""
12-
enqueuing queue 2 using the values from queue 1 bringing them out in First
13-
In First Out (F.I.F.O) order.
14-
"""
15-
self.queue2.append(self.queue1[0])
16-
self.queue1 .pop(0)
17-
"""
18-
adding the new value to queue 1
19-
"""
20-
self.queue1.append(item)
21-
11+
def push(self, item) -> None:
2212
"""
23-
returning the values from queue 2 to queue 1 so as
24-
to replicate the stack data structure
13+
Push an item onto the stack.
14+
15+
>>> stack = StackUsingQueues()
16+
>>> stack.push(1)
17+
>>> stack.push(2)
18+
>>> stack.push(3)
19+
>>> stack.peek()
20+
3
2521
"""
26-
while len(self.queue2)!= 0:
22+
while len(self.queue1) != 0:
23+
self.queue2.append(self.queue1[0])
24+
self.queue1.pop(0)
25+
self.queue1.append(item)
26+
while len(self.queue2) != 0:
2727
self.queue1.append(self.queue2[0])
2828
self.queue2.pop(0)
2929

30+
def pop(self) -> int:
31+
"""
32+
Pop the top item from the stack and return it.
3033
31-
32-
33-
def pop(self):
34+
>>> stack = StackUsingQueues()
35+
>>> stack.push(1)
36+
>>> stack.push(2)
37+
>>> stack.pop()
38+
2
39+
>>> stack.pop()
40+
1
41+
>>> stack.pop()
42+
0
43+
"""
3444
if len(self.queue1) != 0:
35-
"""
36-
The first value of queue 1 is being popped here as it
37-
has been implemented in a way that the
38-
first value of the queue which gets popped is the last value of the stack.
39-
And since stack follows Last In First Out (L.I.F.O) order the
40-
following has been implemented.
41-
"""
4245
popped = self.queue1.pop(0)
4346
return popped
4447
else:
45-
return None
46-
47-
48-
48+
return 0
4949

50-
51-
52-
53-
def peek(self):
50+
def peek(self) -> int:
5451
"""
55-
Function to see the last value inserted in the stack
56-
implemented using queue here, which has the last
57-
value of the stack as its first.
52+
Return the top item from the stack without removing it.
53+
54+
>>> stack = StackUsingQueues()
55+
>>> stack.push(1)
56+
>>> stack.push(2)
57+
>>> stack.peek()
58+
2
59+
>>> stack.pop()
60+
2
61+
>>> stack.peek()
62+
1
63+
>>> stack.pop()
64+
1
65+
>>> stack.peek()
66+
0
5867
"""
5968
if len(self.queue1) != 0:
6069
return self.queue1[0]
6170
else:
62-
return None
71+
return 0
6372

6473

0 commit comments

Comments
 (0)