ohlawd
ohlawd

Reputation: 13

Programming a basic Pascal Triangle in Python

I'm 4th week into programming(python) and it's getting serious. Our professor asked us to programme a field in python, existing of fields of increasing length for the representation of the triangle of Pascal:

b = [[1],[1,1],[1,2,1],[1,3,3,1],...]

We are just beginning with basics of coding, meaning we are not allowed to use any add-ons like functions or the like. For the most, we worked with while- and for-loops, and with the if statements.

I would really need help to get started here. I began like this:

n = int(input("Number of layers="))
b = [[1]]

for layer in range(0,n):
     for row in range(0,n):

and here is the point where I am stuck.

I see the structure in the way the triangle of pascale is presented: As far as I can see, the length of the fields inside of b grows by 1 with each additional layer. I had an idea, that I could check for outside elements, because they always keep the value "1". I just don't find a solution, how to do that..

1
11
121
1331
14641
..
..

However, I just cannot find a starting point from here on to keep going.. Maybe someone can help me out.. Please keep in mind, that I have to keep it to while- , and for- loops. That is it. No extra functions or something.

Upvotes: 0

Views: 5725

Answers (3)

David
David

Reputation: 122

Im sure there are really nice solution out there but if you are looking for something that is on beginer level check out this

n = 5 #depth of the pascal tree

pascal = []

for x in range(n):

    if x == 0:
        help_list = [1]        
        pascal.append(help_list)
        continue
    if x==1:
        help_list = [1,1]       
        pascal.append(help_list)
        continue

    help_list = [l for l in range(x+1)] #this will just initialize list so you can add to it
    for y in range(1,x):
        help_list[0] = 1
        help_list[x] = 1 
        help_list[y] = pascal[x-1][y] + pascal[x-1][y-1]        

    pascal.append(help_list)

print(pascal)

Upvotes: 1

Dan D.
Dan D.

Reputation: 74655

What is:

    1 3 3 1 0 
  + 0 1 3 3 1
  -----------
    1 4 6 4 1

You can compute the next row by adding the elements of the prior row to themselves after shifting them over by one:

p = [1,3,3,1]
l = [0]*(len(p)+1)
for i in range(len(p)):
    l[i] += p[i]
for i in range(len(p)):
    l[i+1] += p[i]
print l

If [0]*n is not permitted then you can build the list of 0s via:

l = []
for i in range(len(p)+1):
    l += [0]

Upvotes: 1

sigvaldm
sigvaldm

Reputation: 643

Since it's an assignment I won't provide you with a complete solution, but I'll try to help you on the way. It seems like you're suggesting using two nested for loops. This should work, although you might need to change one just the slightest. Remember, you already got b = [[1]].

Next, think about how an element in a new row is constructed from the previous row. Is there a simple algebraic relation to some of the previous elements? (You might want to ask Wikipedia about Pascal's triangle). How do you index those elements? You might also want to look into for instance the append function for lists. This might not be the most efficient solution, but that doesn't matter here.

Upvotes: 0

Related Questions