Created
February 17, 2015 20:03
-
-
Save lapointexavier/7ff868c16ceafe837935 to your computer and use it in GitHub Desktop.
Challenge
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
def availability(start, end, blocks): | |
"""Return a new list of tuples of ints, where those ints | |
represent the time ranges between start and end that | |
don't intersect with any blocks. | |
Keyword arguments: | |
start -- earliest start time we care about | |
end -- latest end time we care about | |
blocks -- list of tuples of ints that we wish to exclude | |
return -- list of tuples of ints | |
""" | |
requested_range = set(range(start, end + 1)) | |
unavailability = set([i for block in blocks for i in range(block[0]+1, block[1])]) | |
availability_blocks = sorted(requested_range - unavailability) | |
pair = [] | |
pairs = [] | |
for i in availability_blocks: | |
if pair and i - pair[-1] > 1: | |
pairs.append((min(pair), max(pair))) | |
pair = [] | |
pair.append(i) | |
pairs.append((min(pair), max(pair))) | |
return pairs | |
print availability(1, 5, [(2,4)]) # [(1, 2), (4, 5)] | |
print availability(1, 5, []) # [(1, 5)] | |
print availability(2, 5, [(1,3)]) # [(3, 5)] | |
print availability(2, 5, [(1,3), (1,3)]) # [(3, 5)] << Redundant unavailability block | |
print availability(1, 10, [(2,6), (5,9)]) # [(1, 2), (9, 10)] << Overlapping unavailability block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment