From e3247efa1926e3bf799871761b3c67d65eaafc59 Mon Sep 17 00:00:00 2001 From: ahmedabougabal Date: Mon, 11 Nov 2024 23:19:10 +0200 Subject: [PATCH] doc: adds some comments for clarification --- .../Exercise Files/07_02_begin/priority_queue.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Ex_Files_Python_Data_Structures/Exercise Files/07_02_begin/priority_queue.py b/Ex_Files_Python_Data_Structures/Exercise Files/07_02_begin/priority_queue.py index 836badb..18eab5c 100644 --- a/Ex_Files_Python_Data_Structures/Exercise Files/07_02_begin/priority_queue.py +++ b/Ex_Files_Python_Data_Structures/Exercise Files/07_02_begin/priority_queue.py @@ -6,7 +6,7 @@ import heapq - +# min heap implementation (smaller numbers have higher priority) class PriorityQueue: def __init__(self): self.elements = [] @@ -16,15 +16,16 @@ def is_empty(self): def put(self, item, priority): # push item into a PQ heapq.heappush(self.elements, (priority, item)) - # lower priority numbers get up first def get(self): - return heapq.heappop(self.elements)[1] + return heapq.heappop(self.elements)[1] # select the number to remove from the tuple + # lower priority numbers are to be removed first (1 before 2 before 3) def __str__(self): return str(self.elements) +# lower numbers --> high priority task as in task scheduling (sys monitor) if __name__ == "__main__": pq = PriorityQueue() # print(pq) @@ -35,6 +36,6 @@ def __str__(self): # print(pq.is_empty()) print(pq) - print(pq.get()) + print(pq.get()) # removed 'code' from pq print(pq)