-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Implemented a Pascal triangle generator #7317
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
Merged
cclauss
merged 13 commits into
TheAlgorithms:master
from
wissamfawaz:pascal-triangle-branch
Oct 30, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
73936f6
Added a Pascal triangle implementation to the other folder
wissamfawaz 2c89234
Added Pascal triangle implementation to the other folder.
wissamfawaz 1a6643b
Added Pascal triangle implementation to the other folder.
wissamfawaz c6a623b
Added Pascal triangle implementation to the other folder.
wissamfawaz c396c3a
Implemented a Pascal triangle generator.
wissamfawaz f9c7e31
Reversed Changes to DIRECTORY.md
wissamfawaz 242d795
Reversed changed to .md files
wissamfawaz cc6be06
Update other/pascal_triangle.py
wissamfawaz 029aabd
Update pascal_triangle.py
wissamfawaz 28dc3e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 115932f
Merge branch 'pascal-triangle-branch' of https://github.com/wissamfaw…
wissamfawaz 4d18c82
Printed output in triangular form
wissamfawaz 58da7d7
Update CONTRIBUTING.md
wissamfawaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
This implementation demonstrates how to generate the | ||
elements of a Pascal's triangle. The element having | ||
a row index of r and column index of c can be derived | ||
as follows: | ||
triangle[r][c] = triangle[r-1][c-1]+triangle[r-1][c] | ||
What is Pascal's triangle? | ||
- It is a triangular array containing binomial coefficients. | ||
Refer to (https://en.wikipedia.org/wiki/Pascal%27s_triangle) | ||
for more info about this triangle. | ||
""" | ||
|
||
|
||
def 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 | ||
""" | ||
triangle = generate_pascal_triangle(num_rows) | ||
for row_idx in range(num_rows): | ||
# Print left spaces | ||
for _ in range(num_rows - row_idx - 1): | ||
print(end=" ") | ||
# Print row values | ||
for col_idx in range(row_idx + 1): | ||
if col_idx != row_idx: | ||
print(triangle[row_idx][col_idx], end=" ") | ||
else: | ||
print(triangle[row_idx][col_idx], end="") | ||
print() | ||
|
||
|
||
def generate_pascal_triangle(num_rows: int) -> list[list[int]]: | ||
""" | ||
Create Pascal's triangle for different number of rows | ||
>>> 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]] | ||
""" | ||
triangle: list[list[int]] = [] | ||
for current_row_idx in range(num_rows): | ||
current_row = populate_current_row(triangle, current_row_idx) | ||
triangle.append(current_row) | ||
return triangle | ||
wissamfawaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int]: | ||
""" | ||
>>> triangle = [[1]] | ||
>>> populate_current_row(triangle, 1) | ||
[1, 1] | ||
""" | ||
current_row = [-1] * (current_row_idx + 1) | ||
# first and last elements of current row are equal to 1 | ||
current_row[0], current_row[-1] = 1, 1 | ||
for current_col_idx in range(1, current_row_idx): | ||
calculate_current_element( | ||
triangle, current_row, current_row_idx, current_col_idx | ||
) | ||
return current_row | ||
|
||
|
||
def 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] | ||
""" | ||
above_to_left_elt = triangle[current_row_idx - 1][current_col_idx - 1] | ||
above_to_right_elt = triangle[current_row_idx - 1][current_col_idx] | ||
current_row[current_col_idx] = above_to_left_elt + above_to_right_elt | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.