-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqueues.py
More file actions
108 lines (74 loc) · 3.25 KB
/
queues.py
File metadata and controls
108 lines (74 loc) · 3.25 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
"""This module holds classes related to FIFO queues.
This module contains `Queue`, an implementation of a FIFO queue, and `QueueUnderflowError`, an error raised by `Queue`.
Examples:
Initializing, adding, and removing elements from a `Queue`:
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.dequeue()
The following code will raise a `QueueUnderflowError` because the second dequeue is done on an empty `Queue`:
q.dequeue()
q.dequeue()
"""
from collections import deque
class QueueUnderflowError(Exception):
"""This class is used by `Queue` to raise errors for operations done on an empty `Queue`."""
def __init__(self, operation):
"""Initializes a `QueueUnderflowError` that will be raised associated with a particular `Queue` operation.
Args:
operation: a string specifying the operation to raise an error on
"""
super().__init__(f'Cannot perform {operation} on an empty queue.')
class Queue:
"""This class represents a FIFO queue that has no maximum capacity.
Examples:
To initialize a `Queue`:
q = Queue()
To add elements to the end of `q`:
q.enqueue(1)
q.enqueue(2)
To remove and return the element at the front of `q` (in this case `x = 1`):
x = q.dequeue()
To see the element at the front of `q` (in this case `y = 2`):
y = q.front()
"""
def __init__(self):
"""Initializes an empty `Queue` in `O(1)` time."""
self.__buf = deque()
def front(self):
"""Gets the element at the front of this `Queue`.
This operation runs in `O(1)` time with respect to the size of this `Queue`.
Returns:
The element at the front of the `Queue`. That is, the element that was first added to this `Queue` of the
elements in it.
Raises:
QueueUnderflowError: If this `Queue` is empty.
"""
if len(self.__buf) == 0:
raise QueueUnderflowError('front()')
return self.__buf[0]
def dequeue(self):
"""Removes the element at the front of this `Queue`.
This operation runs in `O(1)` time with respect to the size of this `Queue`.
Returns:
The element at the front of the `Queue` that was removed. That is, the element that was first added to this
`Queue` of the elements in it.
Raises:
QueueUnderflowError: If this `Queue` is empty.
"""
if len(self.__buf) == 0:
raise QueueUnderflowError('dequeue()')
return self.__buf.popleft()
def enqueue(self, value):
"""Adds an element to the end of this `Queue`.
This operation runs in `O(1)` time with respect to the size of this `Queue`.
Args:
value: Element to add to this `Queue`. It can be of any type.
"""
self.__buf.append(value)
def is_empty(self):
"""Returns `True` if this `Queue` is empty, `False` otherwise in `O(1)` time w/r/t the size of this `Queue`."""
return len(self.__buf) == 0
def size(self):
"""Returns the integer number of elements in this `Queue` in `O(1)` time w/r/t the size of this `Queue`."""
return len(self.__buf)