-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyqueue.py
More file actions
191 lines (142 loc) · 5.24 KB
/
Copy pathmyqueue.py
File metadata and controls
191 lines (142 loc) · 5.24 KB
1
2
3
4
5
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""CSC148 Lab 4: Abstract Data Types
Department of Computer Science,
University of Toronto
=== Module Description ===
In this module, you will develop an implementation of the Queue ADT.
It will be helpful to review the stack implementation from lecture.
"""
from typing import Any
from python_ta.contracts import check_contracts
from collections import deque
# TODO: Task 1
# Implement this class! Note that you'll need at least one private
# attribute to store items.
# A Python list should be used, with items being added to the END of the
# list when enqueuing.
@check_contracts
class Queue:
"""A first-in-first-out (FIFO) queue of items.
Stores data in a first-in, first-out order. When removing an item from the
queue, the first item added is the one that is removed.
"""
# Private Attributes:
_queue_storage: list[Any]
def __init__(self) -> None:
"""Initialize a new empty queue."""
self._queue_storage = []
def is_empty(self) -> bool:
"""Return whether this queue contains no items.
>>> q = Queue()
>>> q.is_empty()
True
>>> q.enqueue('hello')
>>> q.is_empty()
False
"""
return self._queue_storage == []
def enqueue(self, item: Any) -> None:
"""Add <item> to the back of this queue.
"""
self._queue_storage.append(item)
def dequeue(self) -> Any | None:
"""Remove and return the item at the front of this queue.
Return None if this Queue is empty.
(We illustrate a different mechanism for handling an erroneous case.)
>>> q = Queue()
>>> q.enqueue('hello')
>>> q.enqueue('goodbye')
>>> q.dequeue()
'hello'
"""
if self._queue_storage == []:
return None
else:
return self._queue_storage.pop(0)
# TODO: Task 2
# Implement this class! QueueOpposite is similar to Queue, except items
# should be added to the START of the list when enqueueing.
class QueueOpposite:
"""A first-in-first-out (FIFO) queue of items.
Stores data in a first-in, first-out order. When removing an item from the
queue, the first item added is the one that is removed.
"""
# Private Attributes:
_queue_opposite_storage: list[Any]
def __init__(self) -> None:
"""Initialize a new empty queue."""
self._queue_opposite_storage = []
def is_empty(self) -> bool:
"""Return whether this queue contains no items.
>>> q = QueueOpposite()
>>> q.is_empty()
True
>>> q.enqueue('hello')
>>> q.is_empty()
False
"""
return self._queue_opposite_storage == []
def enqueue(self, item: Any) -> None:
"""Add <item> to the back of this queue.
"""
self._queue_opposite_storage.insert(0, item)
def dequeue(self) -> Any | None:
"""Remove and return the item at the front of this queue.
Return None if this Queue is empty.
(We illustrate a different mechanism for handling an erroneous case.)
>>> q = QueueOpposite()
>>> q.enqueue('hello')
>>> q.enqueue('goodbye')
>>> q.dequeue()
'hello'
"""
if self._queue_opposite_storage == []:
return None
else:
return self._queue_opposite_storage.pop()
# TODO: Task 3
# Implement this class!
# Instead of using a Python list, QueueDeque should use the Python "deque"
# object.
# See below for documentation on the deque class:
# https://docs.python.org/3/library/collections.html#collections.deque
class QueueDeque:
"""A first-in-first-out (FIFO) queue of items.
Stores data in a first-in, first-out order. When removing an item from the
queue, the first item added is the one that is removed.
"""
# Private Attributes:
_queue_deque_object_storage: deque[Any]
def __init__(self) -> None:
"""Initialize a new empty queue."""
self._queue_deque_object_storage = deque()
def is_empty(self) -> bool:
"""Return whether this queue contains no items.
>>> q = QueueDeque()
>>> q.is_empty()
True
>>> q.enqueue('hello')
>>> q.is_empty()
False
"""
return len(self._queue_deque_object_storage) == 0
def enqueue(self, item: Any) -> None:
"""Add <item> to the back of this queue.
"""
return self._queue_deque_object_storage.append(item)
def dequeue(self) -> Any | None:
"""Remove and return the item at the front of this queue.
Return None if this Queue is empty.
(We illustrate a different mechanism for handling an erroneous case.)
>>> q = QueueDeque()
>>> q.enqueue('hello')
>>> q.enqueue('goodbye')
>>> q.dequeue()
'hello'
"""
if len(self._queue_deque_object_storage) == 0:
return None
else:
return self._queue_deque_object_storage.popleft()
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)