Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Create Intervals by lukasz.bogaczynski
def create_intervals(numbers):
if len(numbers) < 1:
return []
numbers = sorted(numbers)
intervals = []
current_intervals = [numbers[0], numbers[0]]
for number in numbers[1:]:
if number - current_intervals[1] == 1:
current_intervals[1] = number
else:
intervals.append((current_intervals[0], current_intervals[1]))
current_intervals = [number, number]
intervals.append((current_intervals[0], current_intervals[1]))
return intervals
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. 21, 2017