-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-pascal_triangle.py
39 lines (35 loc) · 1.08 KB
/
12-pascal_triangle.py
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
33
34
35
36
37
38
39
#!/usr/bin/python3
"""Defines a Pascal's Triangle function"""
def pascal_triangle(n):
"""The function returns a list of lists of
integers representing the Pascal’s triangle of n
Args:
n (int): The Pascal number
"""
triangle = []
if n == 0:
return triangle
else:
row = 0
while row < n:
row_list = []
value = 1
if row == 0:
row_list.append(value)
triangle.append(row_list)
elif row == 1:
row_list.append(value)
row_list.append(value)
triangle.append(row_list)
else:
for num in range(row):
if num == 0:
row_list.append(value)
else:
value = (triangle[row - 1][num]) \
+ (triangle[row - 1][num - 1])
row_list.append(value)
row_list.append(1)
triangle.append(row_list)
row += 1
return triangle