DAY 10 OF 100 · Lists · runs of consecutive values

Easy

Compress the Missing Ticket Numbers.

Today’s challenge

Build the solution

Write a function called compress_ranges(numbers).

Return contract: A list of range strings, such as ["1..3", "7", "10..11"].

Input bounds: 0–500 strictly increasing integers from -1,000,000 through 1,000,000.

Keep the supplied driver below your implementation. It reads one JSON object and prints your return value as JSON. The driver may also probe the behaviour described above; those probe fields are part of the test input.

  1. Input numbers are already strictly increasing. Group each maximal consecutive run.
  2. Represent a one-number run as that number in a string. Represent a longer run as start..end, including a run of length two.
  3. Keep the runs in their original order and return an empty list for empty input.

The Twist: Negative values must stay unambiguous: the consecutive run -3, -2, -1 becomes "-3..-1".

Sample

Input

{"numbers":[1,2,3,7,10,11]}

Expected output

["1..3","7","10..11"]
day-10.py
Python 3
Tab inserts four spaces. Press Escape, then Tab to move to the next control, or Shift+Tab to move to the previous control.

Log in or create an account to run code, submit solutions, and save your progress.

Output

Run your code to see the result here.