Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Create Intervals by vvm70
def create_intervals(data):
data = sorted(data)
data = [*data[:1]] + [i for j in [(i, j) for i, j in zip(data[:-1], data[1:]) if j - i > 1] for i in j] + [*data[-1:]]
return [i for i in zip(data[::2], data[1::2])]
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"
print('Almost done! The only thing left to do is to Check it!')
Dec. 5, 2020