-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathpascals_triangle.py
More file actions
48 lines (37 loc) · 1.35 KB
/
pascals_triangle.py
File metadata and controls
48 lines (37 loc) · 1.35 KB
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
40
41
42
43
44
45
46
47
48
"""
Generate Pascal's Triangle.
Reference: https://en.wikipedia.org/wiki/Pascal%27s_triangle
Python doctest can be run with the following command:
python -m doctest -v pascals_triangle.py
Given a non-negative integer num_rows, generate the first num_rows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
"""
class Solution:
def generate(self, num_rows: int) -> list[list[int]]:
"""
Generate the first num_rows of Pascal's triangle.
Args:
num_rows (int): The number of rows to generate.
Returns:
list[list[int]]: Pascal's triangle as a list of lists.
Examples:
>>> solution = Solution()
>>> solution.generate(5)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
>>> solution.generate(1)
[[1]]
>>> solution.generate(0)
[]
"""
ans: list[list[int]] = []
for i in range(num_rows):
# Initialize the row with 1s
row = [1] * (i + 1)
# Compute inner elements by summing elements from the previous row
for j in range(1, i):
row[j] = ans[i - 1][j - 1] + ans[i - 1][j]
ans.append(row)
return ans
if __name__ == "__main__":
import doctest
doctest.testmod()