-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
36 lines (28 loc) · 938 Bytes
/
util.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
import heapq
class PriorityQueue:
def __init__(self, priorityFunction):
self.heap = []
self.count = 0
self.priorityFunction = priorityFunction
def push(self, item):
priority = self.priorityFunction(item)
heapq.heappush(self.heap, (priority, self.count, item))
self.count += 1
def pop(self):
(priority, count, item) = heapq.heappop(self.heap)
return item
def isEmpty(self):
return len(self.heap) == 0
def get_class(module, object_name):
if object_name not in dir(module):
raise AttributeError("{} is not found in {}".format(object_name, module))
return getattr(module, object_name)
def heaptest():
def distanceFrom5(item):
return abs(item-5)
pqueue = PriorityQueue(distanceFrom5)
lst = range(1, 10, 1)
for num in lst:
pqueue.push(num)
while not pqueue.isEmpty():
print(pqueue.pop())