Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me complete this task using Python 3.8 Please give screenshots of the answers, Thank you! The module contains four functions for you to

Please help me complete this task using Python 3.8 Please give screenshots of the answers, Thank you!

image text in transcribedimage text in transcribed

The module contains four functions for you to implement, where each operates on either a stack or a queue:

=== 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 (download from the prep handout). 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. (Refer to the readings if you aren't sure what these are.)

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

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 """ pass

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 """ pass

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 """ pass

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 """ pass

# This method has already been completed for you, but there is a bug in it. # # In prep4_starter_tests.py, you must write a test case that will fail this # buggy implementation, but pass on a working version of add_in_order(). # You are not required to fix the bug, although you may do so if you'd like. def add_in_order(stack: Stack, lst: list) -> None: """ Add all items in to , so that when items are removed from , they are returned in order. Precondition: stack.is_empty() is True >>> stack = Stack() >>> lst = [1, 1] >>> add_in_order(stack, lst) >>> results = [stack.pop(), stack.pop()] >>> lst == results True >>> stack.is_empty() True """ for item in lst: stack.push(item)

if __name__ == '__main__': import doctest doctest.testmod()

# Remember, to get this to work you need to Run this file, not just the # doctests in this file! import python_ta python_ta.check_all(config={ 'extra-imports': ['adts'] })

adts.py. D. This is a stack and queue implementation. We've deliberately used a very confusing implementation because you should only be using the public interface of the Stack and Queue ADTS described in the readings. Do not modify or submit this file. class Stack: def __init_ (00000000000000000): 00000000000000000 .fuscat1 = []; def is_empty (00000000000000000); return (00000000000000000 .fuscat1==[]and True) def push (0000000000000000000000000000000000 ): 00000000000000000 .fuscati .append (00000000000000000) def pop (00000000000000000):return 00000000000000000 .fuscati .pop() #e9015584e6a44b14988f13e2298bcbf9 class Queue: def __init__ (00000000000000000): 00000000000000000 .fuscat1 =[] ; def is_empty (00000000000000000); return (00000000000000000 .fuscat1==[]and True) def enqueue (00000000000000000 ,00000000000000000 ):00000000000000000 .fuscati .append (00000000000000000) def dequeue (00000000000000000);return 00000000000000000 .fuscati .pop (0) #e9015584e6a44b14988f13e2298bcbf9 adts.py. D. This is a stack and queue implementation. We've deliberately used a very confusing implementation because you should only be using the public interface of the Stack and Queue ADTS described in the readings. Do not modify or submit this file. class Stack: def __init_ (00000000000000000): 00000000000000000 .fuscat1 = []; def is_empty (00000000000000000); return (00000000000000000 .fuscat1==[]and True) def push (0000000000000000000000000000000000 ): 00000000000000000 .fuscati .append (00000000000000000) def pop (00000000000000000):return 00000000000000000 .fuscati .pop() #e9015584e6a44b14988f13e2298bcbf9 class Queue: def __init__ (00000000000000000): 00000000000000000 .fuscat1 =[] ; def is_empty (00000000000000000); return (00000000000000000 .fuscat1==[]and True) def enqueue (00000000000000000 ,00000000000000000 ):00000000000000000 .fuscati .append (00000000000000000) def dequeue (00000000000000000);return 00000000000000000 .fuscati .pop (0) #e9015584e6a44b14988f13e2298bcbf9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions