Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 14:08
Show Gist options
  • Save Averroes/43e51c36c354dd4d0cb8 to your computer and use it in GitHub Desktop.
Save Averroes/43e51c36c354dd4d0cb8 to your computer and use it in GitHub Desktop.
implementing a priority queue
# example.py
#
# Example of a priority queue
import heapq
class PriorityQueue:
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]
# Example use
class Item:
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Item({!r})'.format(self.name)
q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1)
print("Should be bar:", q.pop())
print("Should be spam:", q.pop())
print("Should be foo:", q.pop())
print("Should be grok:", q.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment