Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

aid sheet Write this function (outside any class) that takes a Stack and makes a Queue with the same items. Ie should als o clear

image text in transcribed
aid sheet
image text in transcribed
Write this function (outside any class) that takes a Stack and makes a Queue with the same items. Ie should als o clear the Stack. The items should go into the Queue in such a way that the first one that went into the stack is the first one out of the queue. (Of course, any further items will go into the queue via the enqueue method, preserving this FIFO behaviour.) You may make use of the Stack and Queue classes defined on the provided aid sheet to create temporary Stack and/or Queue objects, but you should not create any other new objects such as lists or dictionaries. You must not access the instance attribut es of any Stack or Queue. Use the public interface instead. def make_queue.from(s: Stack) -Queue: ""C Ensure that when the items are dequeued, the first item that vent into the original Stack vill be the first item out of the Queue. lear the Stack s, and return a Queue containing the items that were in s. >>> nums Stack O >>> nums.push(1) >>>nums.push (2) nums.push (3) >>> q make_queue from (nums) >>> nums.is empty True >>>q.enqueue (4) >>> q.dequeue >>> q.dequeue >>> q.dequeue () >>> q.dequeue) >>q.is emptyO True Queues clasa Stack last-in-firat-ost (LIFO) stack of items. claas Queue: of items . (FIFO) queue A first-in-first-out Stores data in a last-in, first-out order ihen removing an item from the stack, the most recently-odded ttem is the one that is removed Stores dats is a firat-in, Jsrat-out order. hes removing as item fron the gweue, the nost recently-added item is the one that is removed. # ms. Private Attributes #-items : # The items stored is this stack. # The end of the list representa # the top of the stack. -iteas: List ,** Private at tribales-_ -ftems: list of the itess is this quese itess: List def init... (self) None Instialize a neu erpty guese. def init..(self) ->None: Initialize a neu empty stack. self..iteas def is espty(self) - bool: ""Return whether this queue contains no itens def is eapty(aelt) - bool: ""Return ahether this atack contains no items 0sese() >>g.iserptyo Trve > Stack) >>s.isemptyO True >>> s.push(hello' >>> s.is empty) False >g.enguese(hello) >>>g.isempty) Fatse return solt .itess def enqueus (self, ites: Any) ->None: return olf.-itens == [] "Add to the back of this quese def push (self, item: Any) ->None: self. .itess.append (item) ""Add a new element to the top of this stack. solf._item.s append (iten) def dequeue (self) -> Optional [Any]: " "Remove and return the item at the front of this quese def pop(se1f)Any: Return None f this Quese is empty. (Ve illustrate a different mechanism for handling an erroneous case.) """Remove and return the element at the top of this stack. Raise an EnptyStackError f this stack is empty >>>-Queve ) >>> .enguese('hello.) >>>g.enguevel'goodbye) > q.dequeve) >>Stack() >>>a.push ('hello >> s.push('goodbye') >>>s.popO goodbye hello if self.is exptyO return None else: if self.is espty): return sel . items pop(o) raise EmptyStackError else return self..itens.pop)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions