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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
>>> 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: <queue> contains at least one item.
| | | | or: not queue.is_empty()
>>> queue = Queue ()
>>> queue.enqueue (1)
>>> queue.enqueue (2)
>>> queue.enqueue (3)
>>>
>>> queue.is_empty()
False
remove_all_but_one(queue)
>>> queue.dequeue ()
3
>>> queue.is_empty()
Transcribed Image Text:78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 >>> 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: <queue> contains at least one item. | | | | or: not queue.is_empty() >>> queue = Queue () >>> queue.enqueue (1) >>> queue.enqueue (2) >>> queue.enqueue (3) >>> >>> queue.is_empty() False remove_all_but_one(queue) >>> queue.dequeue () 3 >>> queue.is_empty()
prep4.py X
prep4-starter_tests.py
C: > Users > PC > Desktop > prep4.py
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
=== 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
- push()
pop()
Queue
##
is_empty()
-
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.
# Part 1
from typing import Any, Optional
from adts import Stack, Queue
# 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 <stack>.
Precondition: <stack> has at least two items.
>>> stack = Stack()
>>> stack.push (1)
>>> stack.push (2)
>>> reverse_top_two(stack)
>>> stack.pop()
1
>>> stack.pop()
2
Transcribed Image Text:prep4.py X prep4-starter_tests.py C: > Users > PC > Desktop > prep4.py 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 === 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 - push() pop() Queue ## is_empty() - 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. # Part 1 from typing import Any, Optional from adts import Stack, Queue # 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 <stack>. Precondition: <stack> has at least two items. >>> stack = Stack() >>> stack.push (1) >>> stack.push (2) >>> reverse_top_two(stack) >>> stack.pop() 1 >>> stack.pop() 2
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Lock objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education