> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Summary Ranges Python Solution with Tests

> Tested Python solution for LeetCode 228 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 228, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array). [View on LeetCode](https://leetcode.com/problems/summary-ranges/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 228   # by problem number
lcpy gen -s summary_ranges   # by problem name
```

## Problem

You are given a **sorted unique** integer array `nums`.

A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive).

Return *the **smallest sorted** list of ranges that **cover all the numbers in the array exactly***. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.

Each range `[a,b]` in the list should be output as:

* `"a->b"` if `a != b`
* `"a"` if `a == b`

### Examples

```
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
```

**Explanation:** The ranges are:
\[0,2] --> "0->2"
\[4,5] --> "4->5"
\[7,7] --> "7"

```
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
```

**Explanation:** The ranges are:
\[0,0] --> "0"
\[2,4] --> "2->4"
\[6,6] --> "6"
\[8,9] --> "8->9"

### Constraints

* `0 <= nums.length <= 20`
* `-2^31 <= nums[i] <= 2^31 - 1`
* All the values of `nums` are **unique**.
* `nums` is sorted in ascending order.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/summary_ranges/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/summary_ranges/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) excluding output
    def summary_ranges(self, nums: list[int]) -> list[str]:
        result: list[str] = []
        i = 0
        while i < len(nums):
            start = nums[i]
            while i + 1 < len(nums) and nums[i + 1] == nums[i] + 1:
                i += 1
            if start == nums[i]:
                result.append(str(start))
            else:
                result.append(f"{start}->{nums[i]}")
            i += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) excluding output |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.