Created
January 6, 2020 15:44
-
-
Save sakex/287b3eaf5b0c4aed5fe2ca8bbdb17f90 to your computer and use it in GitHub Desktop.
Clément Mihailescu Google mock 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
bounds1 = ('9:00', '20:00') | |
c1 = [('9:00', '10:30'), ('12:00', '13:00'), ('16:00', '18:00')] | |
c2 = [('10:00', '11:30'), ('12:30', '14:30'), ('14:30', '15:00'), ('16:00', '17:00')] | |
bounds2 = ('10:00', '18:30') | |
def parse_date(date: str): | |
h, m = map(int,date.split(":")) | |
return 100*h + m | |
def parse_period(period): | |
start, end = period | |
return parse_date(start), parse_date(end) | |
cal1 = list(map(parse_period, c1)) | |
cal2 = list(map(parse_period, c2)) | |
def available(cal, bounds): | |
periods = [] | |
parse_bounds = parse_date(bounds[0]) | |
if parse_bounds < cal[0][0]: | |
periods.append((parse_bounds, cal[0][0])) | |
length = len(cal) | |
for i in range(1, length): | |
if cal[i][0] > cal[i-1][1]: | |
periods.append((cal[i-1][1], cal[i][0])) | |
parse_bounds = parse_date(bounds[1]) | |
if parse_bounds > cal[-1][1]: | |
periods.append((cal[-1][1], parse_bounds)) | |
return periods | |
cal1 = available(cal1, bounds1) | |
cal2 = available(cal2, bounds2) | |
def split_date(date): | |
return "%d:%d%s" % ((date - date%100)/100, date % 100, "0" if date%100 == 0 else "") | |
def match(cal1, cal2): | |
periods = [] | |
min_index = 0 | |
length2 = len(cal2) | |
for i in cal1: | |
start, end = i | |
for j in range(min_index, length2): | |
period = cal2[j] | |
s2, e2 = period | |
if end < s2: | |
break | |
min_index = j | |
if (start >= s2 and start <= e2) or (s2 >= start and s2 <= end): | |
start, end = ((max(start, s2), min(end, e2))) | |
periods.append((split_date(start), split_date(end))) | |
return periods | |
print(match(cal1, cal2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment