Group Consecutive Numbers
When numbers appear one after another, it is often clearer to show them as a range instead of a long list. The function summarize_ranges receives a sorted array of unique integers array. Combine consecutive numbers into continuous ranges and return them as [start, end] pairs. If a number stands alone, its range should be [x, x].
Примеры
Ввод
[[0, 1, 2, 4, 5, 7]]Вывод
[[0, 2], [4, 5], [7, 7]]Ввод
[[-5, -4, -3, 1, 2, 4, 5, 6]]Вывод
[[-5, -3], [1, 2], [4, 6]]Ввод
[[-2, -1, 0, 1, 2]]Вывод
[[-2, 2]]Нужно реализовать
def summarize_ranges(array: list[int]) -> list[list[int]]:
Теги
array
Kirjaudu sisään nähdäksesi palautushistorian
Kirjaudu sisäänKirjaudu sisään käyttääksesi tekoälymentoria
Kirjaudu sisäänVisible tests
1
Input
[0,1,2,4,5,7]Expected
[[0,2],[4,5],[7,7]]2
Input
[-5,-4,-3,1,2,4,5,6]Expected
[[-5,-3],[1,2],[4,6]]3
Input
[-2,-1,0,1,2]Expected
[[-2,2]]4
Input
[1]Expected
[[1,1]]5
Input
[-2,-1,0,1,2,-2]Expected
[[-2,2],[-2,-2]]6
Input
[7,5,4,2,1,0]Expected
[[7,7],[5,5],[4,4],[2,2],[1,1],[0,0]]Focus radio
Paused · SomaFM · Fluid