matrix.pascal_triangle

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

benchmark(→ None)

Benchmark multiple functions, with three different length int values.

calculate_current_element(→ None)

generate_pascal_triangle(→ list[list[int]])

Create Pascal's triangle for different number of rows

generate_pascal_triangle_optimized(→ list[list[int]])

This function returns a matrix representing the corresponding pascal's triangle

populate_current_row(→ list[int])

print_pascal_triangle(→ None)

Print Pascal's triangle for different number of rows

Module Contents

matrix.pascal_triangle.benchmark() None

Benchmark multiple functions, with three different length int values.

matrix.pascal_triangle.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]
matrix.pascal_triangle.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’

matrix.pascal_triangle.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.

Parameters:

num_rows – Integer specifying the number of rows in the Pascal’s triangle

Returns:

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’

matrix.pascal_triangle.populate_current_row(triangle: list[list[int]], current_row_idx: int) list[int]
>>> triangle = [[1]]
>>> populate_current_row(triangle, 1)
[1, 1]
matrix.pascal_triangle.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