Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Create Intervals by mdroz
def create_intervals(data):
lista=[]
if not data:
return lista
else:
i=0
j=0
data = [int(x) for x in data]
data.sort()
while i < len(data)-1:
if data[i+1] - data[i] == 1:
i+=1
else:
lista.append((data[j],data[i]))
i+=1
j=i
lista.append((data[j],data[i]))
return lista
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!')
Oct. 30, 2017