Created
September 30, 2014 13:13
-
-
Save juanplopes/dfdba66719503011f0c8 to your computer and use it in GitHub Desktop.
Simple scheduling algorithm in python (2nd version)
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
import heapq | |
def schedule(tasks): | |
heap = [] | |
def offer(begin, end): | |
while heap and heap[0] <= begin: | |
heapq.heappop(heap) | |
heapq.heappush(heap, end) | |
return len(heap) | |
return ((offer(begin, end), (begin, end)) for begin, end in sorted(tasks)) | |
for room, meeting in schedule([(12,13), (13,15), (17,20), (13,14), (19, 21), (18, 20)]): | |
print 'Meeting {} should occur in room {}'.format(meeting, room) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment