Last active
July 3, 2019 17:11
-
-
Save jmachadofreitas/19332f395b6e0e0dbe700c644ddf5d57 to your computer and use it in GitHub Desktop.
Heap from "Stop Writing Classes" by Jack Diederich
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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