Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
reduce solution in Clear category for Create Intervals by yoichi
import functools
def reducer(acc, i):
if not acc:
return [(i, i)]
if acc[-1][1] + 1 < i:
return acc + [(i, i)]
updated = (acc[-1][0], i)
return acc[:-1] + [updated]
def create_intervals(data):
"""Create a list of intervals out of set of ints."""
return functools.reduce(reducer, sorted(data), [])
Aug. 3, 2017
Comments: