Merge intervals
You are given ranges of time as (start, end) pairs, in any order. Combine all that overlap so the result covers the same time with the fewest ranges.
merge_intervals([(5, 7), (1, 3), (2, 4)])
# [(1, 4), (5, 7)]
merge_intervals([(1, 3), (3, 5)])
# [(1, 5)]Touching pairs count as overlapping, as the second example shows.
Approach
Sort by start. Now every interval that could overlap the current one comes right after it, so one pass is enough. For each interval, either it starts within the last merged range, which you then extend, or it begins a new range.
Be careful when extending. An interval can sit fully inside another, as (1, 10) and (2, 3) do. Take the larger of the two ends.
Your task
Write merge_intervals(intervals) returning a sorted list of merged (start, end) tuples, merging overlapping and touching pairs, without changing the input.
The explanation opens here once every check passes.