Created
November 28, 2019 19:01
-
-
Save eliascotto/3221542dd5de6681417aaa4be4d14f0c to your computer and use it in GitHub Desktop.
Airbnb Front End Engineer Interview
This file contains 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
''' | |
Given a list of schedules, provide a list of times that are available for a meeting | |
Example input: | |
[ | |
[[4,5],[6,10],[12,14]], | |
[[4,5],[5,9],[13,16]], | |
[[11,14]] | |
] | |
Example Output: | |
[[0,4],[10,11],[16,23]] | |
''' | |
def timesForMeeting(scheds): | |
all_time = set(range(24)) # use set for remove from list without Error | |
for sing in scheds: | |
for schedule in sing: | |
start = schedule[0] | |
stop = schedule[1] | |
while start < stop: | |
start += 1 | |
all_time.discard(start) | |
all_time = list(all_time) # list of hours free 0-24. Each hour use a +1 number, ex 11 means 10-11 | |
free_time = [] | |
sched = [] | |
for i in range(len(all_time)): | |
if sched == []: | |
# if schedule is empty append 0 or current hour -1 | |
if all_time[i] == 0: | |
sched.append(0) | |
else: | |
sched.append(all_time[i] - 1) | |
# if we are in a sequence of hours, skip | |
if i < len(all_time) - 1 and all_time[i + 1] == all_time[i] + 1: | |
continue | |
# otherwhise append the current hour (we ended the free sequence) | |
sched.append(all_time[i]) | |
free_time.append(sched) | |
sched = [] | |
return free_time | |
if __name__ == '__main__': | |
x = timesForMeeting([ | |
[[4,5],[6,10],[12,14]], | |
[[4,5],[5,9],[13,16]], | |
[[11,14]] | |
]) | |
print(x) # [[0, 4], [10, 11], [16, 23]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment