Skip to content

Instantly share code, notes, and snippets.

@TheAlchemistKE
Created June 6, 2023 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAlchemistKE/c081351454e64b04e533e1ce2732db24 to your computer and use it in GitHub Desktop.
Save TheAlchemistKE/c081351454e64b04e533e1ce2732db24 to your computer and use it in GitHub Desktop.
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