heapq is a nice python module, but the interface is not so clean. I found a one-liner on this blog, it's as short as possible, but not really pythonic either :) Here is my contribution to a most readable heap class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import heapq
class Heap(list) :
def __init__(self, * pos_arg, ** nam_arg) :
list.__init__(self, * pos_arg, ** nam_arg)
def pop(self) :
return heapq.heappop(self)
def push(self, item) :
heapq.heappush(self, item)
def pushpop(self, item) :
return heapq.heappushpop(self, item)
def poppush(self, itemp) :
return heapq.replace(self, item)
|
Tags: heap
I wonder if this also supports priorities. I'd also like to see a heapq implemtantion in Python with a more intuative priority ordering.
Sorry, 8-chars indentation looks ugly.
It's not 8 space indented, but one tab :)
As such, you can set your editor to whatever-you-want-and-I-don't-need-to-care tab size. Space indentation kills kittenz :D