Question: 6 . Implement a queue using two stacks. See below... [ 2 . 4 hour challenge ] ` ` ` # Implement a queue using

6. Implement a queue using two stacks. See below... [2.4 hour challenge]
```
# Implement a queue using two stacks ...
# More specifically, fill in the details where it says
# "add your code here" in the following code
# without using any additional storage.
class ChallengeStack:
def __init__(self):
self.data =[]
def push(self, value):
self.data.append(value)
def pop(self):
if len(self.data)==0:
return None
r = self.data[-1]
del self.data[-1]
return r
class ChallengeQueue:
def __init__(self):
```
```
self.s2= ChallengeStack()
def put(self, value):
pass # - add your code here
def get(self):
pass # - add your code here
# show the next element without removing it from the queue
def peek(self):
pass # - add your code here
# returns the total number of elements in the queue
def size(self):
pass # - add your code here
#Test your Queue implementation using the following ...
q = ChallengeQueue()
for i in range(10):
assert q.size()== i
q.put(i)
assert q.size()== i+1
assert q.peek()==0
for i in range(5):
assert q.size()==10-i
assert q.peek()== i
assert q.get()== i
assert q.size()==10-i-
for i in range(10):
assert q.size()== i+5
q.put(i+10)
assert q.size()== i+6
assert q.peek()==5
```
for i in range(15):
assert q.size()==15-i
assert q.peek()== i+5
assert q.get()== i+5
assert q.size()==15-i-1
Submit your python .py file to Moodle.
6 . Implement a queue using two stacks. See

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!