Skip to content

Commit 13d179b

Browse files
authored
Update pascal_triangle.py
1 parent adc0210 commit 13d179b

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

matrix/pascal_triangle.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
https://en.wikipedia.org/wiki/Pascal%27s_triangle
88
"""
99

10-
from typing import List, Callable
10+
from collections.abc import Callable
1111
from timeit import timeit
1212

1313

@@ -35,7 +35,7 @@ def print_pascal_triangle(num_rows: int) -> None:
3535
print()
3636

3737

38-
def generate_pascal_triangle(num_rows: int) -> List[List[int]]:
38+
def generate_pascal_triangle(num_rows: int) -> list[list[int]]:
3939
"""
4040
Generate Pascal's triangle for the specified number of rows
4141
>>> generate_pascal_triangle(0)
@@ -68,14 +68,13 @@ def generate_pascal_triangle(num_rows: int) -> List[List[int]]:
6868
if num_rows < 0:
6969
raise ValueError("Input value 'num_rows' must be >= 0")
7070

71-
triangle: List[List[int]] = []
71+
triangle: list[list[int]] = []
7272
for current_row_idx in range(num_rows):
7373
current_row = populate_current_row(triangle, current_row_idx)
7474
triangle.append(current_row)
7575
return triangle
7676

77-
78-
def populate_current_row(triangle: List[List[int]], current_row_idx: int) -> List[int]:
77+
def populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int]:
7978
"""
8079
>>> triangle = [[1]]
8180
>>> populate_current_row(triangle, 1)
@@ -92,8 +91,8 @@ def populate_current_row(triangle: List[List[int]], current_row_idx: int) -> Lis
9291

9392

9493
def calculate_current_element(
95-
triangle: List[List[int]],
96-
current_row: List[int],
94+
triangle: list[list[int]],
95+
current_row: list[int],
9796
current_row_idx: int,
9897
current_col_idx: int,
9998
) -> None:
@@ -109,14 +108,14 @@ def calculate_current_element(
109108
current_row[current_col_idx] = above_left + above_right
110109

111110

112-
def generate_pascal_triangle_optimized(num_rows: int) -> List[List[int]]:
111+
def generate_pascal_triangle_optimized(num_rows: int) -> list[list[int]]:
113112
"""
114113
Returns a matrix representing Pascal's triangle.
115114
Reduces operations by half by eliminating redundant calculations.
116-
115+
117116
:param num_rows: Number of rows in the Pascal's triangle
118117
:return: 2D list representing the Pascal's triangle
119-
118+
120119
>>> generate_pascal_triangle_optimized(3)
121120
[[1], [1, 1], [1, 2, 1]]
122121
>>> generate_pascal_triangle_optimized(1)
@@ -141,7 +140,7 @@ def generate_pascal_triangle_optimized(num_rows: int) -> List[List[int]]:
141140
if num_rows < 0:
142141
raise ValueError("Input value 'num_rows' must be >= 0")
143142

144-
result: List[List[int]] = [[1]]
143+
result: list[list[int]] = [[1]]
145144

146145
for row_index in range(1, num_rows):
147146
temp_row = [0] + result[-1] + [0]
@@ -159,11 +158,11 @@ def generate_pascal_triangle_optimized(num_rows: int) -> List[List[int]]:
159158
return result
160159

161160

161+
162162
def benchmark() -> None:
163163
"""
164164
Benchmark functions with different input sizes
165165
"""
166-
167166
def benchmark_a_function(func: Callable, value: int) -> None:
168167
call = f"{func.__name__}({value})"
169168
timing = timeit(f"__main__.{call}", setup="import __main__")

0 commit comments

Comments
 (0)