Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # names = ['art','eng','math','cs','music']
- # start_times = [9,9.5,10,10.5,11]
- # end_times = [10,10.5,11,11.5,12]
- names = ['a','b','c','d','e','f','g','h']
- start_times = [0,1,3,3,4,5,6,8]
- end_times = [6,4,5,8,7,9,10,11]
- def solve():
- global names, start_times, end_times
- schedule = []
- while(len(names) != 0):
- # get the earliest end time
- earliest_index = 0
- for i in range(1, len(end_times)):
- if end_times[i] < end_times[earliest_index]:
- earliest_index = i
- schedule.append(names[earliest_index])
- earliest_end_time = end_times[earliest_index]
- # pop off the earliest class for next iteration
- names.pop(earliest_index)
- start_times.pop(earliest_index)
- end_times.pop(earliest_index)
- # getting new lists of valid times
- new_names = []
- new_start = []
- new_end = []
- for i in range(len(start_times)):
- if start_times[i] >= earliest_end_time: # only if the class starts after the last inputted class ends
- new_names.append(names[i])
- new_start.append(start_times[i])
- new_end.append(end_times[i])
- # updates the new classes to consider
- names = new_names
- start_times = new_start
- end_times = new_end
- return schedule
- print(solve())
Add Comment
Please, Sign In to add comment