Created
March 12, 2016 16:28
-
-
Save afnanenayet/0a92e5d541a39c8867e8 to your computer and use it in GitHub Desktop.
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
def return_max_record(timetable): | |
'''This function returns the maximum number of possible non-concurrent | |
recordings from VHS. Input a list with each row formatted: | |
[start time, end time]''' | |
for times in timetable: | |
times.append(times[1] - times[0]) | |
# Maximizing the number | |
timetable = sorted(timetable, key = lambda x: x[2]) | |
recorded_timetable = [] | |
recorded_timetable.append(timetable[0]) | |
for show in timetable: | |
# Fringe cases | |
# Checking beginning | |
if show[1] <= recorded_timetable[0][0]: | |
recorded_timetable.insert(0, show) | |
# Checking end | |
elif show[0] >= recorded_timetable[len(recorded_timetable) - 1][1]: | |
recorded_timetable.append(show) | |
# Seeing if elements fit in "between" any of the shows we've recorded | |
else: | |
for i in range(1, len(recorded_timetable) - 2): | |
if show[1] >= recorded_timetable[i][0] and show[0] <= \ | |
recorded_timetable[i + 1][1]: | |
recorded_timetable.insert(i + 1, show) | |
return len(recorded_timetable) | |
challenge_input = "" | |
input_table = [] | |
filename = input("Enter filename of input: ") | |
with open(filename, "r") as f: | |
for line in f: | |
temp_tuple = line.split() | |
input_table.append([int(temp_tuple[0]), int(temp_tuple[1])]) | |
print(return_max_record(input_table)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment