Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
create_intervals solution in Clear category for Create Intervals by dannedved
def create_intervals(data):
"""
Create a list of intervals out of set of ints.
"""
data = sorted(list(data))
try:
firsts = [data[0]] + [data[i] for i in range(1, len(data)) if data[i] - data[i - 1] > 1]
lasts = [data[i] for i in range(len(data) - 1) if data[i + 1] - data[i] > 1] + [data[-1]]
except IndexError:
return []
return list(zip(firsts, lasts))
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!')
May 3, 2019