forked from OmkarPathak/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP59_PascalTriangle.py
More file actions
32 lines (27 loc) · 822 Bytes
/
P59_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
def pascalRow(n):
if n == 0:
return [1]
else:
N = pascalRow(n-1)
return [1] + [N[i] + N[i+1] for i in range(n-1)] + [1]
# create a triangle of n rows
def pascalTriangle(n):
triangle = []
for i in range(n):
triangle.append(pascalRow(i))
return triangle
if __name__ == '__main__':
for i in pascalTriangle(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]