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.

Revisions

  1. jmachadofreitas revised this gist Jul 3, 2019. No changes.
  2. jmachadofreitas created this gist Jul 3, 2019.
    18 changes: 18 additions & 0 deletions heap.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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]