def collapse_intervals(items): This function is the inverse of the previous question of expanding positive integer intervals. Given a nonempty list of positive integers that is guaranteed to be in sorted ascending order, create and return the unique description string where every maximal sublist of consecutive integers has been condensed to the notation first-last. If some maximal sublist consists of a single integer, it must be included in the result string just by itself without the minus sign separating it from the now redundant last number. Make sure that the string returned by your function does not contain any whitespace characters, and does not have a redundant comma at the end. items expected results [1, 2, 4, 6, 7, 8, 9, 10, 12, 13] '1-2,4,6-10,12-13' [42] '42' [3, 5, 6, 7, 9, 11, 12, 13] '3,5-7,9,11-13' [ ] ' ' range(1, 1000001) '1-1000000' please add comments in between the codes so I understand
def collapse_intervals(items):
This function is the inverse of the previous question of expanding positive integer intervals. Given a nonempty list of positive integers that is guaranteed to be in sorted ascending order, create and return the unique description string where every maximal sublist of consecutive integers has been condensed to the notation first-last. If some maximal sublist consists of a single integer, it must be included in the result string just by itself without the minus sign separating it from the now redundant last number. Make sure that the string returned by your function does not contain any
whitespace characters, and does not have a redundant comma at the end.
items | expected results |
[1, 2, 4, 6, 7, 8, 9, 10, 12, 13] | '1-2,4,6-10,12-13' |
[42] | '42' |
[3, 5, 6, 7, 9, 11, 12, 13] | '3,5-7,9,11-13' |
[ ] | ' ' |
range(1, 1000001) | '1-1000000' |
please add comments in between the codes so I understand
Step by step
Solved in 2 steps with 1 images