Skip to content

Instantly share code, notes, and snippets.

@cod3smith
Created June 6, 2023 13:32
Show Gist options
  • Select an option

  • Save cod3smith/c081351454e64b04e533e1ce2732db24 to your computer and use it in GitHub Desktop.

Select an option

Save cod3smith/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