Created
June 6, 2023 13:32
-
-
Save TheAlchemistKE/c081351454e64b04e533e1ce2732db24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
class PriorityQueue: | |
def __init__(self): | |
self.items = [] | |
self.count = 0 | |
def enqueue(self, item, priority): | |
heapq.heappush(self.items, (priority, self.count, item)) | |
self.count += 1 | |
def dequeue(self): | |
if not self.is_empty(): | |
return heapq.heappop(self.items)[2] | |
def is_empty(self): | |
return len(self.items) == 0 | |
def size(self): | |
return len(self.items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment