Creating triangle-like structure of lists in that hold numbers from bottom left

Viewed 31

I am trying to create a structure that would look like this, the number of items it could hold would be n:

7
4 8
2 5 9
1 3 6 10

And converting it to a list like so:

[[1, 3, 6, 10],[2, 5, 9],[4, 8],[7]]

I can't wrap my head around it, I figured each item increases by 1, then 2 then 3 and so on, but the way I want them to store is something I can't figure out.

fn(6)

[[1 3 6],[2 5],[4]]
1 Answers

I must be the most kind person ever, but it did seem like a fun thing to test out. Here is a function that works:

def fn(input):
    output = []

    # First row:
    row = [1]
    incrementor = 2
    while row[-1]+incrementor <= input:
        row.append(row[-1]+incrementor)
        incrementor += 1
    output.append(row)

    # Rest of rows:
    for i in range(len(output[0])-1):
        output.append([e-1 for e in output[-1][1:]])


print(fn(10))

>> [[1, 3, 6, 10], [2, 5, 9], [4, 8], [7]]
Related