Trouble understanding this pascals triangle generator

def print_pascal_triangle(size):
    for i in range(0, size):
        for j in range(0, i + 1):
            print(decide_number(i, j), end=" ")
        print()


def decide_number(n, k):
    num = 1
    if k > n - k:
        k = n - k
    for i in range(0, k):
        num = num * (n - i)
        num = num // (i + 1)
    return num

# set rows
rows = 7
print_pascal_triangle(rows)

On the first line I’m getting the values for n and k to be 0 because i and j start at 0, what am I missing? the subsequent operations in the decide_number function then are confusing if everything is 0 to start with…

Nothing, n and k are indeed both 0 in the first iteration.

Let’s walk through it:

def decide_number(n, k):  # n = k = 0
    num = 1
    if 0 > 0 - 0:  # False
        k = n - k
    for i in range(0, 0):  # range(0, 0) contains nothing, so the loop does not run.
        num = num * (n - i)
        num = num // (i + 1)
    return num  # num == 1

So, print(decide_number(0, 0), end=" ") becomes print(1, end=" ").

Thanks for the reply! I’m trying to work it out and I think I get the first two rows being 1 and 1 1 respectively but on the third row (i=2) I cant seem to get the line 1 2 1.

So for that row I get i=2 and j in range (0,3). The first 1 is no problem.

When j=1, i=2 (still) so n=2, k=1
k>n-k
1>2-1 false

So for i in range (0,1):
num=1*(2-1)=1
num=1//(i+1)=…0?

Not sure what I’m missing…

This is a strange way to generate Pascal’s Triangle, since each row derives so very easily from the previous row. I’d do it something more like this:

def print_pascal_triangle(size):
    row = []
    for x in range(size):
        # Generate a new row from the previous row, by adding pairs.
        # There are many ways to do this. I'll use a very very simple one
        # but there are much fancier ways available if you're interested.
        new_row = [1]
        for i in range(len(row) - 1):
            new_row.append(row[i] + row[i + 1])
        if x: new_row.append(1)
        print(*new_row)
        row = new_row

But since you have the version that seems to be based on the “n choose m” usage, I would strongly recommend using something like pythontutor.com to help you visualize what your code is doing.

1 Like

You’re missing that the first value of i is zero, so it’s actually:

for i in range (0, k):  # k == 1
     num = num * (n - i)  # num == 1, n == 2, i == 0 -> num = 1 * (2 - 0) == 2
     num = num // (i+1)  # num == 2, i == 0 -> num = 2 // 1 == 2

Thanks for the reply, this is an assignment question that I found, thats why its not a very efficient code. I’m just wanting to understand the code as shown. I’ll look into that site, I appreciate the susggestion.

Amazing! Now its all clear, just a simple mistake. I love how its all being made more clear as I learn. If I had to come up with the code to create the pattern I’d be lost- I have no idea how I could come up with such a logic process to create the pascal patten but its good to understand how the code generates it finally. I really appreciate your help.

Set aside the code. Can you describe to us, your audience, what a Pascal
triangle is, and how the triangle of size n+1 is related to the
triangle of size n? In Plain English?

Try to describe how each value in the next triangle is computed from the
previous. Since each triangle is the previous triangle with an
additional row, can you describe how to make the additional row?

There’s a nice animation on this page:

Write something like that out as English instructions for a person.

Then see if they have any correspondence to the code you were debugging,
or if they suggest steps you could write as code.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Yep, that’s what I would consider the standard way to render the whole triangle. But the original example was using the “n choose k” meaning of Pascal’s Triangle (see Pascal's triangle - Wikipedia from the same Wikipedia page you linked to), which is equally valid, but less efficient.

Aye.

I was more trying to convey an approach to “making Python code to do
something” for Dave to try out.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like