Both in Python language : Question 1: Question 2: Write a function, mirror_queue
ID: 3713374 • Letter: B
Question
Both in Python language :
Question 1:
Question 2:
Write a function, mirror_queue(a), which takes a Queue, q, as a parameter and modifies the queue so that the queue items appear in their original order followed by a copy of the queue items in reverse order. You must make use of a stack to help you to solve this problem. The implementations of both the Stack and Queue ADTs are provided to you. You can simply use: push0, pop), is empty0 as well as enqueue0 and dequeue0 as necessary in your function definition. However, you will need to use the following statement to create a Stack (in this question) s my_stack_module.Stack) Similarly, you will need to use the following statement to create a Queue (in this question) q-my queue_module.Queue) For example Test Result q1-my_queue_module.Queue q1.enqueue(1) q1.enqueue(2) q1.enqueue(3) mirror_queue(q1) print(q1) Queue: [1, 2, 3, 3, 2, 1] q1-my_queue_module.Queue Queue: mirror_queue(ql) print(q1) Answer: (penalty regime: 0 %) 1def mirror_queue(orig_queue): backup_s-my_stack_module.StackO 3 backup_q - my_queue_module.QueueExplanation / Answer
Ques 1.
def mirror(orig_queue):
# create a backup stack
backup_s = my_stack_module.Stack()
# create a backup queue
backup_q = my_stack_module.Queue()
# loop untill queue is empty
while orig_queue.is_empty() == False:
# dequeue element from orig_queue
x = orig_queue.dequeue()
# add the dequeued element to backup queue
backup_q.enqueue( x )
# add the dequeued element to backup stack
backup_s.push( x )
# add the eement in original order
# loop untill backup queue is empty
while backup_q.is_empty() == False:
# dequeue element from backup queue
x = backup_q.dequeue()
# add the dequeued element to original queue
orig_queue.enqueue( x )
# add the eement in reverse order
# loop untill backup stack is empty
while backup_s.is_empty() == False:
# pop the top most element from backup stack
x = backup_s.pop()
# add the dequeued element to original queue
orig_queue.enqueue( x )
Ques 2.
def reverse_queue(q):
# create a backup stack
backup_s = my_stack_module.Stack()
# loop untill queue is empty
while q.is_empty() == False:
# dequeue element from q
x = q.dequeue()
# add the dequeued element to backup stack
backup_s.push( x )
# add the eement in reverse order
# loop untill backup stack is empty
while backup_s.is_empty() == False:
# pop the top most element from backup stack
x = backup_s.pop()
# add the dequeued element to original queue
q.enqueue( x )
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.