Skip to content

Instantly share code, notes, and snippets.

@bmwant
Created December 8, 2015 13:18
Show Gist options
  • Save bmwant/40dc7b0b36bd9fc28851 to your computer and use it in GitHub Desktop.
Save bmwant/40dc7b0b36bd9fc28851 to your computer and use it in GitHub Desktop.
Rooms and timetable problem
timetable = (
('10:30', '11:30'),
('10:00', '11:00'),
('11:00', '12:00'),
('09:33', '12:00'),
('17:00', '19:00'),
('17:00', '19:00'),
('17:00', '19:00'),
('17:00', '19:00'),
)
def normalize_timetable(timetable):
converted_timetable = convert_timetable(timetable)
normalized_timetable = sort_timetable(converted_timetable)
return normalized_timetable
def convert_timetable(timetable):
converted_timetable = []
for start_time, end_time in timetable:
start_hours, start_minutes = start_time.split(':')
start_in_minutes = int(start_hours)*60 + int(start_minutes)
end_hours, end_minutes = end_time.split(':')
end_in_minutes = int(end_hours)*60 + int(end_minutes)
converted_timetable.append((start_in_minutes, end_in_minutes))
return converted_timetable
def get_max_simultaneous_meetings(timetable):
max_rooms_occupied = 1 # At least one room need to be present
for index, meeting in enumerate(timetable):
previous_meetings = timetable[:index]
rooms_occupied = 1
current_start_time, _ = meeting
for prev_meeting in previous_meetings:
_, prev_end_time = prev_meeting
if current_start_time < prev_end_time:
rooms_occupied += 1
if rooms_occupied > max_rooms_occupied:
max_rooms_occupied = rooms_occupied
return max_rooms_occupied
def sort_timetable(timetable):
# Specify sorting key explicitly besides it is not neccessary
return sorted(timetable, key=lambda x: x[0])
if __name__ == '__main__':
#print(convert_timetable(timetable))
nt = normalize_timetable(timetable)
print(get_max_simultaneous_meetings(nt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment