-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpq.py
55 lines (38 loc) · 1.24 KB
/
pq.py
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
class Item:
def __init__(self, val, priority):
self.val = val
self.priority = priority
def __repr__(self):
return "Item(val: {}, pri: {})".format(self.val,
self.priority)
class PriorityQueue:
def __init__(self):
self.storage = []
def __str__(self):
return "[" + ", ".join([str(item) for item in self.storage]) + "]"
def standard_priority_func(self, p1, p2):
return p1 < p2
def insert(self, val, priority):
to_insert = Item(val, priority)
insert_index = 0
while insert_index < len(self.storage):
if self.standard_priority_func(self.storage[insert_index].priority, priority):
break
insert_index += 1
self.storage.insert(insert_index, to_insert)
def peek(self):
return self.storage[-1].val
def pop(self):
return self.storage.pop().val
def simple_test():
print("Running simple test...")
pq = PriorityQueue()
pq.insert("c", 3)
pq.insert("a", 1)
pq.insert("b", 2)
assert(pq.pop() == "a")
assert(pq.pop() == "b")
assert(pq.pop() == "c")
print("Simple test passed!")
if __name__ == "__main__":
simple_test()