Summer reading list
Joe Cheng
code at joecheng.com
Tue Aug 12 11:56:11 EDT 2003
> Accordingly, I suggest the following works of literature:
>
> * heapq.py (255 lines)
> * sets.py (536 lines)
> * textwrap.py (355 lines)
> * csv.py (427 lines)
>
> These make enjoyable reading, cover interesting topics/algorithms,
> demonstrate superb modern python technique, and showcase
> effective use of Python's newer features.
I read heapq.py from here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Lib/he
apq.py
Quoting from the comments:
"""Usage:
heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0] # smallest item on the heap without popping it
heapify(x) # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
# new item; the heap size is unchanged"""
It might just be my Java background creeping in (I'm a Python newbie), but,
wouldn't it be better if this was OO?
heap = Heap()
heap.push(item)
item = heap.pop()
item = heap[0]
heapified = Heap(x)
item = heap.replace(item)
Otherwise the user could easily break the heap by doing something dumb to
the list...
More information about the Python-list
mailing list