forked from chiphuyen/coding-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.py
More file actions
40 lines (26 loc) · 729 Bytes
/
priority_queue.py
File metadata and controls
40 lines (26 loc) · 729 Bytes
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
import random
from binary_heap import BinaryHeap
class PriorityQueue(object):
def __init__(self):
self._heap = BinaryHeap()
def peek(self):
return self._heap.peek_min()
def is_empty(self):
return self._heap.is_empty()
def enqueue(self, value):
self._heap.insert(value)
def dequeue(self):
self._heap.extract_min()
def __iter__(self):
yield from iter(self._heap)
def __len__(self):
return len(self._heap)
def test_priority_queue():
pq = PriorityQueue()
values = random.sample(range(-15, 15), 30)
for v in values:
pq.enqueue(v)
print(list(pq))
for v in iter(pq):
print(v)
test_priority_queue()