|
| 1 | +""" |
| 2 | +This implementation demonstrates how to generate the |
| 3 | +elements of a Pascal's triangle. The element having |
| 4 | +a row index of r and column index of c can be derived |
| 5 | +as follows: |
| 6 | +triangle[r][c] = triangle[r-1][c-1]+triangle[r-1][c] |
| 7 | +What is Pascal's triangle? |
| 8 | +- It is a triangular array containing binomial coefficients. |
| 9 | +Refer to (https://en.wikipedia.org/wiki/Pascal%27s_triangle) |
| 10 | +for more info about this triangle. |
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | +def print_pascal_triangle(num_rows: int) -> None: |
| 15 | + """ |
| 16 | + Print Pascal's triangle for different number of rows |
| 17 | + >>> print_pascal_triangle(5) |
| 18 | + 1 |
| 19 | + 1 1 |
| 20 | + 1 2 1 |
| 21 | + 1 3 3 1 |
| 22 | + 1 4 6 4 1 |
| 23 | + """ |
| 24 | + triangle = generate_pascal_triangle(num_rows) |
| 25 | + for row_idx in range(num_rows): |
| 26 | + # Print left spaces |
| 27 | + for _ in range(num_rows - row_idx - 1): |
| 28 | + print(end=" ") |
| 29 | + # Print row values |
| 30 | + for col_idx in range(row_idx + 1): |
| 31 | + if col_idx != row_idx: |
| 32 | + print(triangle[row_idx][col_idx], end=" ") |
| 33 | + else: |
| 34 | + print(triangle[row_idx][col_idx], end="") |
| 35 | + print() |
| 36 | + |
| 37 | + |
| 38 | +def generate_pascal_triangle(num_rows: int) -> list[list[int]]: |
| 39 | + """ |
| 40 | + Create Pascal's triangle for different number of rows |
| 41 | + >>> generate_pascal_triangle(1) |
| 42 | + [[1]] |
| 43 | + >>> generate_pascal_triangle(2) |
| 44 | + [[1], [1, 1]] |
| 45 | + >>> generate_pascal_triangle(3) |
| 46 | + [[1], [1, 1], [1, 2, 1]] |
| 47 | + >>> generate_pascal_triangle(4) |
| 48 | + [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]] |
| 49 | + >>> generate_pascal_triangle(5) |
| 50 | + [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] |
| 51 | + """ |
| 52 | + triangle: list[list[int]] = [] |
| 53 | + for current_row_idx in range(num_rows): |
| 54 | + current_row = populate_current_row(triangle, current_row_idx) |
| 55 | + triangle.append(current_row) |
| 56 | + return triangle |
| 57 | + |
| 58 | + |
| 59 | +def populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int]: |
| 60 | + """ |
| 61 | + >>> triangle = [[1]] |
| 62 | + >>> populate_current_row(triangle, 1) |
| 63 | + [1, 1] |
| 64 | + """ |
| 65 | + current_row = [-1] * (current_row_idx + 1) |
| 66 | + # first and last elements of current row are equal to 1 |
| 67 | + current_row[0], current_row[-1] = 1, 1 |
| 68 | + for current_col_idx in range(1, current_row_idx): |
| 69 | + calculate_current_element( |
| 70 | + triangle, current_row, current_row_idx, current_col_idx |
| 71 | + ) |
| 72 | + return current_row |
| 73 | + |
| 74 | + |
| 75 | +def calculate_current_element( |
| 76 | + triangle: list[list[int]], |
| 77 | + current_row: list[int], |
| 78 | + current_row_idx: int, |
| 79 | + current_col_idx: int, |
| 80 | +) -> None: |
| 81 | + """ |
| 82 | + >>> triangle = [[1], [1, 1]] |
| 83 | + >>> current_row = [1, -1, 1] |
| 84 | + >>> calculate_current_element(triangle, current_row, 2, 1) |
| 85 | + >>> current_row |
| 86 | + [1, 2, 1] |
| 87 | + """ |
| 88 | + above_to_left_elt = triangle[current_row_idx - 1][current_col_idx - 1] |
| 89 | + above_to_right_elt = triangle[current_row_idx - 1][current_col_idx] |
| 90 | + current_row[current_col_idx] = above_to_left_elt + above_to_right_elt |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + import doctest |
| 95 | + |
| 96 | + doctest.testmod() |
0 commit comments