matrix.pascal_triangle ====================== .. py:module:: matrix.pascal_triangle .. autoapi-nested-parse:: This implementation demonstrates how to generate the elements of a Pascal's triangle. The element havingva row index of r and column index of c can be derivedvas follows: triangle[r][c] = triangle[r-1][c-1]+triangle[r-1][c] A Pascal's triangle is a triangular array containing binomial coefficients. https://en.wikipedia.org/wiki/Pascal%27s_triangle Functions --------- .. autoapisummary:: matrix.pascal_triangle.benchmark matrix.pascal_triangle.calculate_current_element matrix.pascal_triangle.generate_pascal_triangle matrix.pascal_triangle.generate_pascal_triangle_optimized matrix.pascal_triangle.populate_current_row matrix.pascal_triangle.print_pascal_triangle Module Contents --------------- .. py:function:: benchmark() -> None Benchmark multiple functions, with three different length int values. .. py:function:: calculate_current_element(triangle: list[list[int]], current_row: list[int], current_row_idx: int, current_col_idx: int) -> None >>> triangle = [[1], [1, 1]] >>> current_row = [1, -1, 1] >>> calculate_current_element(triangle, current_row, 2, 1) >>> current_row [1, 2, 1] .. py:function:: generate_pascal_triangle(num_rows: int) -> list[list[int]] Create Pascal's triangle for different number of rows >>> generate_pascal_triangle(0) [] >>> generate_pascal_triangle(1) [[1]] >>> generate_pascal_triangle(2) [[1], [1, 1]] >>> generate_pascal_triangle(3) [[1], [1, 1], [1, 2, 1]] >>> generate_pascal_triangle(4) [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]] >>> generate_pascal_triangle(5) [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] >>> generate_pascal_triangle(-5) Traceback (most recent call last): ... ValueError: The input value of 'num_rows' should be greater than or equal to 0 >>> generate_pascal_triangle(7.89) Traceback (most recent call last): ... TypeError: The input value of 'num_rows' should be 'int' .. py:function:: generate_pascal_triangle_optimized(num_rows: int) -> list[list[int]] This function returns a matrix representing the corresponding pascal's triangle according to the given input of number of rows of Pascal's triangle to be generated. It reduces the operations done to generate a row by half by eliminating redundant calculations. :param num_rows: Integer specifying the number of rows in the Pascal's triangle :return: 2-D List (matrix) representing the Pascal's triangle Return the Pascal's triangle of given rows >>> generate_pascal_triangle_optimized(3) [[1], [1, 1], [1, 2, 1]] >>> generate_pascal_triangle_optimized(1) [[1]] >>> generate_pascal_triangle_optimized(0) [] >>> generate_pascal_triangle_optimized(-5) Traceback (most recent call last): ... ValueError: The input value of 'num_rows' should be greater than or equal to 0 >>> generate_pascal_triangle_optimized(7.89) Traceback (most recent call last): ... TypeError: The input value of 'num_rows' should be 'int' .. py:function:: populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int] >>> triangle = [[1]] >>> populate_current_row(triangle, 1) [1, 1] .. py:function:: print_pascal_triangle(num_rows: int) -> None 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