Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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
"""
Enter code here
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
"""
Enter code here
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
"""
Enter code here
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
"""
Enter code here
pass
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']
})
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started