Created
December 18, 2018 22:28
-
-
Save nathanhinchey/61a041168f57c0530711f34c98162e40 to your computer and use it in GitHub Desktop.
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
# Given a list of tuples which represent (movie_start_time, movie_end_time), | |
# write a function which will return whether or not there are any overlaps between the movies | |
movies = [ | |
(1, 3), | |
(2, 5), | |
(8, 9), | |
(5, 8), | |
(4, 7), | |
] | |
def scheduler(movies): | |
for i in range(len(movies)): | |
for j in range(i + 1, len(movies)): | |
current_movie = movies[i]; | |
compare_movie = movies[j]; | |
if current_movie[0] < compare_movie[0] < current_movie[1] or compare_movie[0] < current_movie[0] < compare_movie[1] or compare_movie == current_movie: | |
return True | |
return False | |
# print(scheduler(movies)) | |
# test cases | |
no_overlap = [(0,1),(1,2),(10,110)] | |
equal = [(0,1),(0,1)] | |
first_starts_earlier_than_second_and_overlaps = [(0,2),(1,3)] | |
opposite = [(1,3),(0,2)] | |
print(scheduler(no_overlap)) # false | |
print(scheduler(equal)) | |
print(scheduler(first_starts_earlier_than_second_and_overlaps)) | |
print(scheduler(opposite)) | |
""" | |
------------------------------------------------------------------------------------------- | |
""" | |
# Given a list of tuples which represent (movie_start_time, movie_end_time), | |
# write a function which will return whether or not there are any overlaps between the movies | |
movies = [ | |
(1, 3), | |
(2, 5), | |
(8, 9), | |
(5, 8), | |
(4, 7), | |
] | |
def theater_count(movies): | |
most_overlaps = 0 | |
for i in range(len(movies)): | |
current_overlaps = 0 | |
for j in range(i + 1, len(movies)): | |
current_movie = movies[i]; | |
compare_movie = movies[j]; | |
if current_movie[0] < compare_movie[0] < current_movie[1] or compare_movie[0] < current_movie[0] < compare_movie[1] or compare_movie == current_movie: | |
current_overlaps += 1 | |
if current_overlaps > most_overlaps: | |
most_overlaps = current_overlaps | |
return most_overlaps + 1 | |
# print( theater_count(movies)) | |
# test cases | |
no_overlap = [(0,1),(1,2),(10,110)] # 1 | |
equal = [(0,1),(0,1)] # 2 | |
first_starts_earlier_than_second_and_overlaps = [(0,2),(1,3)] # 2 | |
opposite = [(1,3),(0,2)] # 2 | |
three_equal = [(0,1),(0,1),(0,1)] # 3 | |
middle_overlaps_first_and_last = [(0,2),(1,3),(2,4)] # 2 | |
print( theater_count(no_overlap)) # false | |
print( theater_count(equal)) | |
print( theater_count(first_starts_earlier_than_second_and_overlaps)) | |
print( theater_count(opposite)) | |
print( theater_count(three_equal)) | |
print( theater_count(middle_overlaps_first_and_last)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment