MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / print_pascal_triangle

Function print_pascal_triangle

matrix/pascal_triangle.py:11–32  ·  view source on GitHub ↗

Print Pascal's triangle for different number of rows >>> print_pascal_triangle(5) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

(num_rows: int)

Source from the content-addressed store, hash-verified

9
10
11def print_pascal_triangle(num_rows: int) -> None:
12 """
13 Print Pascal's triangle for different number of rows
14 >>> print_pascal_triangle(5)
15 1
16 1 1
17 1 2 1
18 1 3 3 1
19 1 4 6 4 1
20 """
21 triangle = generate_pascal_triangle(num_rows)
22 for row_idx in range(num_rows):
23 # Print left spaces
24 for _ in range(num_rows - row_idx - 1):
25 print(end=" ")
26 # Print row values
27 for col_idx in range(row_idx + 1):
28 if col_idx != row_idx:
29 print(triangle[row_idx][col_idx], end=" ")
30 else:
31 print(triangle[row_idx][col_idx], end="")
32 print()
33
34
35def generate_pascal_triangle(num_rows: int) -> list[list[int]]:

Callers

nothing calls this directly

Calls 1

generate_pascal_triangleFunction · 0.85

Tested by

no test coverage detected