Question: === Module Description === This module contains four functions for you to implement, where each operates on either a stack or a queue. We've provided

=== Module Description ===

This module contains four functions for you to implement, where each

operates on either a stack or a queue.

We've provided deliberately confusing implementations of these ADTs in

adts.py. This is because we don't want you to care at all about the

implementations of these classes, but instead ONLY use the public methods

defined in by the Stack or Queue ADTs. These are the following:

Stack

- is_empty()

- push()

- pop()

Queue

- is_empty()

- enqueue()

- dequeue()

In particular, this means that you shouldn't try to access any attributes

of either class, since the ADT descriptions only define what *operations*

(methods) can be used for the ADTs.

GENERAL HINT: save values in local variables! Even if you pop an item off of

a stack, it's not "gone forever" if you assign it to a variable.

"""

from typing import Any, Optional

from adts import Stack, Queue

################################################################################

# Part 1

# In this part of the prep, you will various Stack and Queue functions.

#

# You must NOT access any attributes of the Stack/Queues passed into each

# function.

#

# You may ONLY use the is_empty(), push(), and pop() methods of Stack, and

# the is_empty(), enqueue(), and dequeue() methods of Queue.

################################################################################

def peek(stack: Stack) -> Optional[Any]:

"""Return the top item on the given stack.

If the stack is empty, return None.

Unlike Stack.pop, this function should leave the stack unchanged when the

function ends. You can (and should) still call pop and push, just make

sure that if you take any items off the stack, you put them back on!

>>> stack = Stack()

>>> stack.push(1)

>>> stack.push(2)

>>> peek(stack)

2

>>> stack.pop()

2

"""

# TODO: Implement this function.

def reverse_top_two(stack: Stack) -> None:

"""Reverse the top two elements on .

Precondition: has at least two items.

>>> stack = Stack()

>>> stack.push(1)

>>> stack.push(2)

>>> reverse_top_two(stack)

>>> stack.pop()

1

>>> stack.pop()

2

>>> stack.is_empty()

True

"""

# TODO: Implement this function.

def remove_all(queue: Queue) -> None:

"""Remove all items from the given queue.

>>> queue = Queue()

>>> queue.enqueue(1)

>>> queue.enqueue(2)

>>> queue.enqueue(3)

>>> remove_all(queue)

>>> queue.is_empty()

True

"""

# TODO: Implement this function.

def remove_all_but_one(queue: Queue) -> None:

"""Remove all items from the given queue except the last one.

Precondition: contains at least one item.

or: not queue.is_empty()

>>> queue = Queue()

>>> queue.enqueue(1)

>>> queue.enqueue(2)

>>> queue.enqueue(3)

>>> remove_all_but_one(queue)

>>> queue.is_empty()

False

>>> queue.dequeue()

3

>>> queue.is_empty()

True

"""

# TODO: Implement this function.

Could someone please help me complete the #TODO part, please only help with implementing four (peek, reverse_top_two, remove_all, remove_all_but_one) functions in Part 1.

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 Databases Questions!