Write a function to merge meeting times given everyone's schedules


Topic: Write a function to merge meeting times given everyone's schedules

Solution

def merge_ranges(meetings):
    sorted_meetings = sorted(meetings)
    merged_meetings = [sorted_meetings[0]]
    for current_meeting_start, current_meeting_end in sorted_meetings[1:]:
        last_merged_meeting_start, last_merged_meeting_end = merged_meetings[-1]
        if (current_meeting_start <= last_merged_meeting_end):
            merged_meetings[-1] = (last_merged_meeting_start,
                                   max(last_merged_meeting_end,
                                       current_meeting_end))
        else:
            merged_meetings.append((current_meeting_start, current_meeting_end))
    return merged_meetings



List all Python Programs