-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
54 lines (47 loc) · 1.46 KB
/
test.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
import heapq
# Initialize and function to add element and remove elements in queue
class PriorityQueue:
def __init__(self, capacity):
self.capacity = capacity
self.queue = []
self.counter = 0
# Maintain only the #of elements in queue as per capacity and remove the low priority item in queue when high priority comes
def enqueue(self, value, priority):
if len(self.queue) < self.capacity:
heapq.heappush(self.queue, (priority, self.counter, value))
self.counter += 1
else:
min_priority, _, _ = self.queue[0]
if priority > min_priority:
heapq.heappop(self.queue)
heapq.heappush(self.queue, (priority, self.counter, value))
self.counter += 1
# Remove the high priority element in queue
def dequeue(self):
sorted_queue = sorted(self.queue, key=lambda x: x[0], reverse=True)
self.queue = sorted_queue
if self.queue:
_, _, value = heapq.heappop(self.queue)
return value
else:
return None
def print_queue(self):
final_sort = sorted(self.queue, key=lambda x: x[0], reverse=True)
for k in final_sort:
print(f"{k[2]} {k[0]}")
print('')
# Read the capacity of the queue
capacity = int(input())
priority_queue = PriorityQueue(capacity)
# Call enqueue / dequeue to add/remove elements in queue
while True:
try:
operation = input().split()
if operation[0] == "enqueue":
value, priority = int(operation[1]), int(operation[2])
priority_queue.enqueue(value, priority)
elif operation[0] == "dequeue":
priority_queue.dequeue()
except EOFError:
break
priority_queue.print_queue()