In [1]:
#Priority Queue with Integers
class PriorityQueueInt:
def __init__(self):
self.queue = []
def __str__(self):
return '''' ''''.join([str(i) for i in self.queue])
def __repr__(self):
return f"PriorityQueueInt({self.queue})"
def is_empty(self):
return len(self.queue) == 0
def push(self, item):
self.queue.append(item)
self.queue.sort(reverse=True)
def pop(self):
if not self.is_empty():
return self.queue.pop(0)
raise IndexError("pop from empty priority queue")
pq_int = PriorityQueueInt()
pq_int.push(10)
pq_int.push(5)
pq_int.push(20)
print(pq_int)
print(pq_int.pop())
20 10 5 20
In [2]:
#Priority Queue with Strings
class PriorityQueueStr:
def __init__(self):
self.queue = []
def __str__(self):
return '''' ''''.join(self.queue)
def __repr__(self):
return f"PriorityQueueStr({self.queue})"
def is_empty(self):
return len(self.queue) == 0
def push(self, item):
self.queue.append(item)
self.queue.sort(reverse=True)
def pop(self):
if not self.is_empty():
return self.queue.pop(0)
raise IndexError("pop from empty priority queue")
# Example usage
pq_str = PriorityQueueStr()
pq_str.push("apple")
pq_str.push("banana")
pq_str.push("cherry")
print(pq_str)
print(pq_str.pop())
cherry banana apple cherry
In [3]:
import pprint
#Priority Queue with Dictionaries (using healthcare data)
class PriorityQueueDict:
def __init__(self):
self.queue = []
def __str__(self):
return '''' ''''.join([str(i) for i in self.queue])
def __repr__(self):
return f"PriorityQueueDict({self.queue})"
def is_empty(self):
return len(self.queue) == 0
def push(self, item):
self.queue.append(item)
self.queue.sort(key=lambda x: x[''''priority''''], reverse=True)
def pop(self):
if not self.is_empty():
return self.queue.pop(0)
raise IndexError("pop from empty priority queue")
# Example usage
pq_dict = PriorityQueueDict()
pq_dict.push({"patient": "John Doe", "priority": 2})
pq_dict.push({"patient": "Jane Smith", "priority": 5})
pq_dict.push({"patient": "Alice Brown", "priority": 3})
pprint.pp(pq_dict)
pprint.pp(pq_dict.pop())
PriorityQueueDict([{''''patient'''': ''''Jane Smith'''', ''''priority'''': 5}, {''''patient'''': ''''Alice Brown'''', ''''priority'''': 3}, {''''patient'''': ''''John Doe'''', ''''priority'''': 2}]) {''''patient'''': ''''Jane Smith'''', ''''priority'''': 5}
In [4]:
#Using heapq
import heapq
import pprint
class PriorityQueueHeap1:
def __init__(self):
self.queue = []
def push(self, item):
heapq.heappush(self.queue, item)
def pop(self):
return heapq.heappop(self.queue) if not self.is_empty() else None
def is_empty(self):
return len(self.queue) == 0
def __str__(self):
return str(self.queue)
def __repr__(self):
return f"PriorityQueueHeap({self.queue})"
class PriorityQueueHeap2:
def __init__(self):
self.queue = []
self.index = 0
def push(self, item, priority):
heapq.heappush(self.queue, (-priority, self.index, item))
self.index += 1
def pop(self):
return heapq.heappop(self.queue)[-1]
def is_empty(self):
return len(self.queue) == 0
def __str__(self):
return str(self.queue)
def __repr__(self):
return f"PriorityQueueHeap({self.queue})"
# Example usage with integers
pq = PriorityQueueHeap1()
pq.push(3)
pq.push(1)
pq.push(2)
print(pq)
print(pq.pop())
print(pq)
# Example usage with strings
pq = PriorityQueueHeap1()
pq.push("banana")
pq.push("apple")
pq.push("cherry")
print(pq)
print(pq.pop())
print(pq)
# Example usage with dictionaries (Healthcare Data)
print("\n++\nDictionary")
pq = PriorityQueueHeap2()
pq.push({''''patient'''': ''''P2'''', ''''priority'''': 2},2)
pq.push({''''patient'''': ''''P1'''', ''''priority'''': 1},1)
pq.push({''''patient'''': ''''P3'''', ''''priority'''': 3},3)
pprint.pp(pq)
pprint.pp(pq.pop())
pprint.pp(pq)
pprint.pp(pq.pop())
pprint.pp(pq)
pprint.pp(pq.pop())
pprint.pp(pq)
[1, 3, 2] 1 [2, 3] [''''apple'''', ''''banana'''', ''''cherry''''] apple [''''banana'''', ''''cherry''''] ++ Dictionary PriorityQueueHeap([(-3, 2, {''''patient'''': ''''P3'''', ''''priority'''': 3}), (-1, 1, {''''patient'''': ''''P1'''', ''''priority'''': 1}), (-2, 0, {''''patient'''': ''''P2'''', ''''priority'''': 2})]) {''''patient'''': ''''P3'''', ''''priority'''': 3} PriorityQueueHeap([(-2, 0, {''''patient'''': ''''P2'''', ''''priority'''': 2}), (-1, 1, {''''patient'''': ''''P1'''', ''''priority'''': 1})]) {''''patient'''': ''''P2'''', ''''priority'''': 2} PriorityQueueHeap([(-1, 1, {''''patient'''': ''''P1'''', ''''priority'''': 1})]) {''''patient'''': ''''P1'''', ''''priority'''': 1} PriorityQueueHeap([])