I am a beginner in python and I have to do a special version of pascal triangle...special in a way that the elements don't add up but they multiply.
here is the example: so the function is defined as multiplicative_pascal(start: int, height: int) -> List[List[int]]
for start = 2
and height = 5
the function should return
[[2 ], [2, 2], [2, 4, 2], [2, 8, 8, 2], [2, 16, 64, 16, 2]]
My thought process and code
First I tried to make normal pascal triangle and then I thought I will just substitute the + with * and 1(the normal starting point for pascal triangle) with the variable start
def multiplicative_pascal(start: int, height: int) -> List[List[int]]:
Pascal_list =[[start]] #FIrst entry Defined to start
for i in range(start, height+1):
temp_list =[]
for j in range(i+1): #+1 As we are considering last element
if(j==0):#First Element = start
temp_list.append(start)
continue
elif(j == i):#Last Element = start
temp_list.append(start)
continue
else:
temp_list.append(Pascal_list[i-1][j]+Pascal_list[i-1][j-1]) # Addition of Upper Two Elements
Pascal_list.append(temp_list)
return Pascal_list
print(multiplicative_pascal(1, 5))
for this function it printed what it should here is the result
[[1 ], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
but when I try to change from print(multiplicative_pascal(1, 5)) to print(multiplicative_pascal(2, 5)) it gives me this error and I am not sure why error if you don't wan't to go to external link :
*Traceback (most recent call last): File "C:\Users\ASUS\Documents\Dalšie štúdium!!\MUNI\Informatika\ucenie na semestralny test.py", line 108, in print(multiplicative_pascal(2, 5)) File "C:\Users\ASUS\Documents\Dalšie štúdium!!\MUNI\Informatika\ucenie na semestralny test.py", line 105, in multiplicative_pascal temp_list.append(Pascal_list[i-1][j]+Pascal_list[i-1][j-1]) # Addition of Upper Two Elements IndexError: list index out of range
*
could someone please help me with this error? or how would you try to solve this problem? maybe my whole thought process is wrong...