-1

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...

Matus
  • 43
  • 5
  • Show requirements, code, output and/or possible error messages as properly formatted text in the question, not as image or by external link. – Michael Butscher Nov 05 '19 at 22:26
  • `Pascal_list[i-1]`. Note `for i in range(start, height+1): `, so on the first pass it becomes `Pascal_list[start - 1]`. At first, there is only one element in that list: `Pascal_list =[[start]]`, so using a `start` value of `2` results in you trying to access one element beyond the size of that list. – Alexander Nov 05 '19 at 22:32
  • Thank you for your commend, I think I understand, but I seem not to be able to fix it. so essentially if my start value > 1 the Pascal_list[start - 1]....the -1 is not enough? – Matus Nov 05 '19 at 22:49

1 Answers1

0

Perhaps something like this will help you:

def multiplicative_pascal(start: int, height: int) -> List[List[int]]:
    if height < 1:
        return []
    result = row = [start]
    for _ in range(height - 1):
        row = [start] + [(a * b) for a, b in zip(row, row[1:])] + [start]
        result.append(row)
    return result

>>> multiplicative_pascal(1, 5)
[[1], [1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1]]  # 1 * 1 is always 1...

>>> multiplicative_pascal(2, 5)
[[2], [2, 2], [2, 4, 2], [2, 8, 8, 2], [2, 16, 64, 16, 2]]
Alexander
  • 105,104
  • 32
  • 201
  • 196