Skip to content

Instantly share code, notes, and snippets.

@flopezluis
Created October 6, 2011 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flopezluis/1267749 to your computer and use it in GitHub Desktop.
Save flopezluis/1267749 to your computer and use it in GitHub Desktop.
Testing heapq
from heapq import heappush, heappop
def heapsort(iterable, priorities={}):
"""
Sorted the secuence according to the priorities
the biggest number is the less prioritary
>>> priorities ={'Como': 1}
>>> sec = ['camino', 'Como', 'test']
>>> heapsort(sec, priorities)
['camino', 'test', 'Como']
>>> priorities ={'Como': 1, 'camino': 2}
>>> heapsort(sec, priorities)
['test', 'Como', 'camino']
"""
h = []
for value in iterable:
heappush(h, (priorities.get(value,0), value))
return [heappop(h)[1] for i in range(len(h))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment