Skip to content
Advertisement

Python: How to convert a list of integers into nested lists of integers where max-min for each nested list never exceeds X?

I’m actually having a hard time explaining this problem. The title ask for this:

limit = 100
l = [1, 2, 4, 9, 33, 77, 85, 100, 151, 304, 405, 407, 499]

do_something(l, limit)

[
[1,2,4,9,33,77,85,100],
[151],
[304],
[405, 407, 499]
]

But that was just one step in my thought process. Truth is, I need this result:

limit = 100
l = [1, 2, 4, 9, 33, 77, 85, 100, 151, 304, 405, 407, 499]

do_something(l, limit)

[
range(1,101),
range(151,152),
range(304,305),
range(405,500)
]

Notice range never exceeds the limit of 100, and its creating a list out of ints from l that fall within the limit (starting from l[0] and resetting any time limit is exceeded). I am brainstorming ideas over here, but everything I can think of iterates over my list several times. If that’s necessary so be it, but I’d like to be a bit more efficient than that.

Thanks for any help

Advertisement

Answer

Loop through the sorted list. Each time you start a group, save the first value of the range in one variable. Then as you step through the list, update the last value of the range to the current element. Whenever the element exceeds first+limit, you start a new range.

def do_something(l, limit):
    if l == []:
        return []
    end = l[0] + limit
    result = []
    first = last = l[0]
    for i in sorted(l):
        if i < end:
            last = i
        else:
            result.append(range(first, last + 1))
            first = last = i
            # reset the limit
            end = i + limit
    # append the last sublist
    result.append(range(first, last+1))
    return result
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement