Skip to contentKEEP THE CRAFTTHE SANCTUARY

Normalize Closed Intervals

Sort closed intervals and merge every pair that overlaps or touches.

The problem

Implement normalize_closed_intervals(intervals). Each interval is a closed range [start, end] with start <= end, so it includes both ends. Merge every group of intervals that overlap or touch: [1, 2] and [2, 4] share the point 2, so they become [1, 4]. Return the merged intervals as new [start, end] lists sorted by start. The input can be empty and can be in any order. Do not change the input list or the intervals inside it.

Starter code

def normalize_closed_intervals(intervals):
    pass

Public examples

  1. intervals = [[5, 7], [1, 3], [2, 6], [10, 12]]gives[[1, 7], [10, 12]]
  2. intervals = [[1, 2], [2, 4], [6, 6]]gives[[1, 4], [6, 6]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.