Skip to content

Instantly share code, notes, and snippets.

@lapointexavier
Created February 17, 2015 20:03
Show Gist options
  • Save lapointexavier/7ff868c16ceafe837935 to your computer and use it in GitHub Desktop.
Save lapointexavier/7ff868c16ceafe837935 to your computer and use it in GitHub Desktop.
Challenge
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