Created
October 6, 2011 15:47
-
-
Save flopezluis/1267749 to your computer and use it in GitHub Desktop.
Testing heapq
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
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