Question: Please complete the four functions for you to implement using Python 3.8, where each operates on either a stack or queue. Then, finish the test
Please complete the four functions for you to implement using Python 3.8, where each operates on either a stack or queue. Then, finish the test cases part. Thank you so much. Please provide screenshots of the answer:


=== 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'] }) Please write at least 1 additional test case for the provided function add_in_order():
=== Module description === This module contains sample tests for Prep 4. You may use these to test your code.
Complete the TODO in this file.
When writing a test case, make sure you create a new function, with its name starting with "test_". For example:
def test_my_test_case(): # Your test here """ from typing import List
from hypothesis import given from hypothesis.strategies import integers, lists
from adts import Stack, Queue from prep4 import peek, reverse_top_two, remove_all, remove_all_but_one, \ add_in_order
# TODO: Implement at least 1 test case for add_in_order() that will # fail on the provided (buggy) implementation but pass on a correct # implementation.
# Below are provided sample test cases for your use. You are encouraged # to add additional test cases. # WARNING: THIS IS AN EXTREMELY INCOMPLETE SET OF TESTS! # Add your own to practice writing tests and to be confident your code is # correct. # For more information on hypothesis (one of the testing libraries we're using), # please see # def test_peek_doctest() -> None: """This is the doctest given in peek.""" stack = Stack() stack.push(1) stack.push(2) assert peek(stack) == 2 assert stack.pop() == 2
@given(lists(integers(), min_size=1, max_size=100)) def test_peek_general(items: List[int]) -> None: """Test that peek works for a large range of stack sizes.""" stack = Stack() for item in items: stack.push(item) assert peek(stack) == items[-1] assert stack.pop() == items[-1]
def test_reverse_top_two_doctest() -> None: """This is the doctest given in reverse_top_two.""" stack = Stack() stack.push(1) stack.push(2) reverse_top_two(stack) assert stack.pop() == 1 assert stack.pop() == 2 assert stack.is_empty()
def test_remove_all_doctest() -> None: """This is the doctest given in remove_all.""" queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) remove_all(queue) assert queue.is_empty()
def test_remove_all_but_one_doctest() -> None: """Test is the doctest given in remove_all_but_one.""" queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) remove_all_but_one(queue) assert queue.is_empty() is False assert queue.dequeue() == 3 assert queue.is_empty()
if __name__ == '__main__': import pytest pytest.main(['prep4_starter_tests.py'])
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
Get step-by-step solutions from verified subject matter experts
