Skip to content

Create pascals_triangle.py #11421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions maths/pascals_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""/**
* Pascal's Triangle is an array of binomial coefficients. It can be used for unwrapping terms like

Check failure on line 2 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/pascals_triangle.py:2:89: E501 Line too long (99 > 88)
* (a + b)^5.
* To construct Pascal's Triangle you add the numbers above the child entry together. Here are the first five rows:

Check failure on line 4 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/pascals_triangle.py:4:89: E501 Line too long (115 > 88)
* 1
* 1 1
* 1 2 1
* 1 3 3 1
* 1 4 6 4 1
*
* Time Complexity: quadratic (O(n^2)).
* @see https://en.wikipedia.org/wiki/Pascal's_triangle
*/ """

# Importing the numpy library as np for numerical operations
import numpy as np

def pascal(n: int):

Check failure on line 18 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

maths/pascals_triangle.py:16:1: I001 Import block is un-sorted or un-formatted
"""
Generate Pascal's triangle up to the n-th row.
Parameters:
n (int): The number of rows in Pascal's triangle to generate.
Returns:
np.ndarray: A 2D numpy array containing the first n rows of Pascal's triangle.
"""
# Initialize a 2D numpy array with zeros, with shape (n,n) and integer type
pascal_array = np.zeros((n, n), dtype=np.int64)
# Set the first column of all rows to 1 (the edge values of Pascal's triangle)
pascal_array[:, 0] = 1

Check failure on line 30 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

maths/pascals_triangle.py:30:1: W293 Blank line contains whitespace
# Fill the pascal_array with the correct values for Pascal's triangle
for i in range(1, n):
# Each element except the edges is the sum of the two directly above it
# pascal_array[i, 1:n] = pascal_array[i-1, 1:n] + pascal_array[i-1, 0:n-1]
pascal_array[i,1:i+1] = pascal_array[i-1,1:i+1] + pascal_array[i-1,0:i]

Check failure on line 36 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

maths/pascals_triangle.py:36:1: W293 Blank line contains whitespace
return pascal_array

def show_pascal(pascal_array):
"""
Print Pascal's triangle in a formatted manner.
Parameters:
pascal_array (np.ndarray): A 2D numpy array containing Pascal's triangle.
"""
# Get the number of rows and columns (should be equal)
n, m = pascal_array.shape[:2]
# Ensure the array is square (n == m), as required for Pascal's triangle
assert n == m

Check failure on line 49 in maths/pascals_triangle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

maths/pascals_triangle.py:49:1: W293 Blank line contains whitespace
# Loop through each row and print the values up to the current row index
for i in range(n):
for j in range(i + 1):
print(pascal_array[i, j], end=" ")
print()

# Main execution block
if __name__ == '__main__':
# Display Pascal's triangle for 10 rows
show_pascal(pascal(10))
Loading