I'm trying to create a pascal's triangle generator, but for some reason it's not giving me the correct output. I should get:
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
But instead I'm getting:
[1, 1]
[1, 2, 1]
[1, 3, 4, 1]
[1, 4, 8, 9, 1]
Here's the code:
length = 4
lst = [1]
for i in range(length):
for j in range(i):
lst[j+1] = temp[j] +temp[j+1]
lst.append(1)
temp = lst
print(lst)
I've been looking at it for hours and I couldn't spot anything wrong with it. I even wrote down the first couple of iterations on paper and it all seems just fine. But somehow it's still not working? It might be me being blind again but I've really got no clue.
I'm so confused, why is it adding the entries from the current list when I created a temporary list to get values from?
There should be a line added that clones the list since right now you are only referencing it.
This produces correct output