Skip to content

Instantly share code, notes, and snippets.

@jmachadofreitas
Last active July 3, 2019 17:11
Show Gist options
  • Save jmachadofreitas/19332f395b6e0e0dbe700c644ddf5d57 to your computer and use it in GitHub Desktop.
Save jmachadofreitas/19332f395b6e0e0dbe700c644ddf5d57 to your computer and use it in GitHub Desktop.
Heap from "Stop Writing Classes" by Jack Diederich
class Heap(object):
def __init__(self, data=None, key=lambda x: None):
"""
Args:
key: scoring function
"""
self.heap = data or []
heapq.heapify(self.heap)
self.key = key
def pushleft(self, item) :
if self.key:
item = (self.key(item), item)
heapq.pushleft(self.heap, item)
def popleft(self):
return heapq.popleft(self.heap)[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment