30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
def create_intervals(data: set):
|
|
"""
|
|
Create a list of intervals out of set of ints.
|
|
"""
|
|
if len(data) == 0:
|
|
return []
|
|
results = []
|
|
data = sorted(data)
|
|
start = None
|
|
current_number = None
|
|
for number in data:
|
|
if start is None:
|
|
start = number
|
|
current_number = start
|
|
continue
|
|
if number != start and number != (current_number + 1):
|
|
results.append((start, current_number))
|
|
start = number
|
|
current_number = number
|
|
results.append((start, current_number))
|
|
return results
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
|
assert create_intervals({1, 2, 3, 4, 5, 7, 8, 12}) == [(1, 5), (7, 8), (12, 12)], "First"
|
|
assert create_intervals({1, 2, 3, 6, 7, 8, 4, 5}) == [(1, 8)], "Second"
|
|
assert create_intervals({7, 9, 10, 11, 12}) == [(7, 7), (9, 12)], "Third"
|
|
print('Almost done! The only thing left to do is to Check it!')
|