알고리즘 문제/CodeWar
Pascal's Triangle #2
BEstyle
2022. 11. 17. 14:34
DESCRIPTION:
Here you will create the classic Pascal's triangle.
Your function will be passed the depth of the triangle and your code has to return the corresponding Pascal's triangle up to that depth.
The triangle should be returned as a nested array. For example:
pascal(5) -> [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
To build the triangle, start with a single 1 at the top, for each number in the next row you just take the two numbers above it and add them together, except for the edges, which are all 1. e.g.:
1
1 1
1 2 1
1 3 3 1
def pascal(p):
if p==1:
return [[1]]
elif p==2:
return [[1],[1,1]]
alist=[[1],[1,1]]
temp=[1,1]
for j in range(p-2):
temp2=[]
temp2.append(1)
for i in range(len(temp)-1):
temp2.append(temp[i]+temp[i+1])
temp2.append(1)
alist.append(temp2)
temp=temp2.copy()
return alist