Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Create Intervals by sawako.oono
def create_intervals(data):
data = sorted(data)
if data == []:
res = []
elif len(data) == 1:
res =[(data[0],data[0])]
else:
res = [[data[0]]]
for n in range(len(data)-1):
if data[n+1] ==data[n]+1:
pass
else:
res[-1].append(data[n])
res.append([data[n+1]])
res[-1].append(data[-1])
res = [tuple(p) for p in res]
return res
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
print(create_intervals({1, 2, 3, 4, 5, 7, 8, 12}) )
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!')
May 29, 2021
Comments: