Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Create Intervals (generator version) by PythonLearner
def create_intervals(data):
numbers = sorted(data)
left = None
right = None
for i in range(len(numbers)):
if i == 0 or numbers[i]-numbers[i-1] > 1:
left = numbers[i]
if i == len(data)-1 or numbers[i+1]-numbers[i] > 1:
right = numbers[i]
if left and right:
interval = (left, right)
left = None
right = None
yield interval
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert list(create_intervals({1, 2, 3, 4, 5, 7, 8, 12})) == [(1, 5), (7, 8), (12, 12)], "First"
assert list(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!')
Sept. 3, 2018