Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Create Intervals by wo.tomasz
def create_intervals(data):
res = []
ls = sorted(list(data))
if len(ls) == 0:
return res
st_e = ls[0]
for i in range(1,len(ls)):
if ls[i-1] != ls[i]-1:
res.append((st_e, ls[i-1]))
if i == len(ls):
break
else:
st_e = ls[i]
res.append((st_e, ls[-1]))
return res
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!')
Feb. 12, 2022