Created
February 17, 2015 20:05
-
-
Save lapointexavier/000419385c6751a32c43 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 | |
""" | |
pair = [] | |
pairs = [] | |
current = start | |
unavailability = set([i for block in blocks for i in range(block[0] + 1, block[1])]) | |
while current >= start and current <= end: | |
valid = current not in unavailability | |
if valid: | |
pair.append(current) | |
elif not valid and pair: | |
pairs.append((min(pair), max(pair))) | |
pair = [] | |
current += 1 | |
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(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