- Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathP59_PascalTriangle.py
More file actions
Latest commit
32 lines (27 loc) · 822 Bytes
/
Copy pathP59_PascalTriangle.py
File metadata and controls
32 lines (27 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Author: OMKAR PATHAK
# PASCAL TRAINGLE: To build the triangle, start with "1" at the top, then continue placing numbers
# below it in a triangular pattern. Each number is the numbers directly above it added together.
# generates the nth row of Pascal's Triangle
defpascalRow(n):
ifn==0:
return [1]
else:
N=pascalRow(n-1)
return [1] + [N[i] +N[i+1] foriinrange(n-1)] + [1]
# create a triangle of n rows
defpascalTriangle(n):
triangle= []
foriinrange(n):
triangle.append(pascalRow(i))
returntriangle
if__name__=='__main__':
foriinpascalTriangle(7):
print(i)
# OUTPUT:
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# [1, 6, 15, 20, 15, 6, 1]