2021/1/30 https://mcs.utm.utoronto.ca/~148/preps/prep4/starter-code/prep4.py https://mcs.utm.utoronto.ca/~148/preps/prep4/starter-code/prep4.py 1/2 """Prep 4 Synthesize === CSC148 Winter 2021 === Department of Mathematical and Computational Sciences, University of Toronto Mississauga === 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 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 swap_top_two(stack: Stack) -> None: """Swap the top two elements on Precondition: has at least two items.
>>> stack = Stack()
def has_at_least(queue: Queue, n: int) -> bool:
"""Return true iff queue contains at least n items.
Precondition: n >= 0
>>> queue = Queue()
>>> has_at_least(queue, 3)
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()
if __name__ == '__main__':
# import doctest
# Remember, to get this to work you need to Run this file, not just the
# doctests in this file!