0

I am a beginner in Python. Now I am trying to create an algorithm that will build a Pascal's triangle.

My first step is to create a basic iteration that will use a list called current level and create the next level list based on it as follows:

  1. Add 0 to the first number to create the left number and insert it into a list.
  2. Create the inner row of numbers and insert them into a list
  3. Add 0 to the last number to create the right number and insert it into a list.

My code includes only one iteration, but iteration is not a problem now. I do not understand how to address the elements of the list by their index correctly.

Can somebody give me a hint?

Thanks!

currentlevel = [1]

nextlevel = []
leftnumber = 0 + currentlevel[0]
nextlevel.append(leftnumber)

for item[index] in currentlevel:
    if index > 1 and index < len(currentlevel)-1:
        item[index] = item[index] + item[index+1]
        nextlevel.append(item[index])
    else:
        rightnumber = currentlevel[-1] + 0
        nextlevel.append(rightnumber)

currentlevel = nextlevel
print(currentlevel)
Jacinda
  • 4,932
  • 3
  • 26
  • 37
naninuneno
  • 23
  • 1
  • 5

3 Answers3

1
  1. This does not work:

    for item[index] in currentlevel:
    

    Use this:

    for index, item in enumerate(currentlevel):
    

    and then wherever you are using item[index] use either item or currentlevel[index]. Except...

  2. Except here:

    item[index] = item[index] + item[index+1]
    nextlevel.append(item[index]) 
    

    Here it would be best if you used another variable:

    next_item = currentlevel[index] + currentlevel[index+1]
    nextlevel.append(next_item) 
    

    Although item would work, it is slightly confusing; but if you use currentlevel[index] where next_item is, you'd be destroying the previous row. Not a problem if you want to print-and-forget, but it's nice to have a choice and be flexible.

  3. This will skip two elements unnecesarily:

    if index > 1 and index < len(currentlevel)-1:
    

    Remove the first clause:

    if index < len(currentlevel)-1:
    

That's all I see for now, can't test.

EDIT: Not an error as such, but 0 + whatever is same as whatever.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Unless I'm missing some Python novelty, the line for item[index] in currentlevel: is a syntax error (edit: it's not a syntax error but a name error; see Tutleman's comment).

Try the following alternative to get both the list element and its index:

for index, item in enumerate(currentlevel):

Or use the following one to get just the indexes and access the elements through them:

for index in range(len(currentlevel)):
    item = currentlevel[index]

Check this answer for more information.

Community
  • 1
  • 1
Racso
  • 2,310
  • 1
  • 18
  • 23
  • 3
    You're right about the `for item[index]` being an error but it's a `NameError`, not a `SyntaxError`. After all, it's perfectly valid to do something like: `x=[1,2,3]; for x[0] in x:`. The behavior of that is really weird (assuming you don't modify the list anywhere else in your loop, at the end, `x` will equal `[3,2,3]`), because you're reassigning the first value of `x` to whatever the next value in `x` is, but you can do it. – Tutleman Jan 05 '15 at 23:19
  • Thanks a lot! This has helped. – naninuneno Jan 06 '15 at 12:08
0

I don't normally post full answers but...

maxlevel = 5
currentlevel = [1]

for levels in range(maxlevel):
    nextlevel = [1]
    for index,item in enumerate(currentlevel[0:-1]):
        if (len(currentlevel) == 1):
            break

        nextlevel.append(item + currentlevel[index + 1])

    nextlevel.append(1)

    print(nextlevel)

    currentlevel = nextlevel
Srdjan Grubor
  • 2,605
  • 15
  • 17