DESCRIPTION:
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients expressed with formula
(nk)=n!k!(n−k)!\lparen {n \atop k} \rparen = \frac {n!} {k!(n-k)!}
where n denotes a row of the triangle, and k is a position of a term in the row.

You can read Wikipedia article on Pascal's Triangle for more information.
Task
Write a function that, given a depth n, returns n top rows of Pascal's Triangle flattened into a one-dimensional list/array.
Example:
n = 1: [1]
n = 2: [1, 1, 1]
n = 4: [1, 1, 1, 1, 2, 1, 1, 3, 3, 1]
def pascals_triangle(n):
if n==1:
return [1]
elif n==2:
return [1,1,1]
ans=[1, 1,1]
alist=[1,1]
templist=[]
for i in range(n-2):
templist.append(1)
for i in range(len(alist)-1):
templist.append(alist[i]+alist[i+1])
templist.append(1)
alist=templist.copy()
for i in alist:
ans.append(i)
templist=[]
return ans
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
Length of missing array (0) | 2022.10.24 |
---|---|
Kebabize (0) | 2022.10.24 |
Tic-Tac-Toe Checker (0) | 2022.10.21 |
Not very secure (0) | 2022.10.21 |
Pyramid Array (0) | 2022.10.20 |