-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Modified 'pascal_triangle.py' program #7901
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
757c5b3
Added pascals_triangle.py program to maths directory
TechFreak107 6f485ac
Deleted 'pascals_triangle.py' because of duplication. Added a optimiz…
TechFreak107 59bffe4
Modified type check hints in 'generate_pascal_triangle_optimized' fun…
TechFreak107 88fa497
Modified 'pascal_triangle' prgram
TechFreak107 4fa2b70
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c90d92a
Update pascal_triangle.py
cclauss 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -38,6 +38,8 @@ def print_pascal_triangle(num_rows: int) -> None: | |||||||||
def 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) | ||||||||||
|
@@ -48,7 +50,26 @@ def generate_pascal_triangle(num_rows: int) -> list[list[int]]: | |||||||||
[[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' | ||||||||||
""" | ||||||||||
|
||||||||||
if not isinstance(num_rows, int): | ||||||||||
raise TypeError("The input value of 'num_rows' should be 'int'") | ||||||||||
|
||||||||||
if num_rows == 0: | ||||||||||
return [] | ||||||||||
elif num_rows < 0: | ||||||||||
raise ValueError( | ||||||||||
"The input value of 'num_rows' should be greater than or equal to 0" | ||||||||||
) | ||||||||||
|
||||||||||
triangle: list[list[int]] = [] | ||||||||||
for current_row_idx in range(num_rows): | ||||||||||
current_row = populate_current_row(triangle, current_row_idx) | ||||||||||
|
@@ -90,6 +111,66 @@ def calculate_current_element( | |||||||||
current_row[current_col_idx] = above_to_left_elt + above_to_right_elt | ||||||||||
|
||||||||||
|
||||||||||
def 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' | ||||||||||
""" | ||||||||||
|
||||||||||
if not isinstance(num_rows, int): | ||||||||||
raise TypeError("The input value of 'num_rows' should be 'int'") | ||||||||||
|
||||||||||
if num_rows == 0: | ||||||||||
return [] | ||||||||||
elif num_rows < 0: | ||||||||||
raise ValueError( | ||||||||||
"The input value of 'num_rows' should be greater than or equal to 0" | ||||||||||
) | ||||||||||
|
||||||||||
result: list[list[int]] = [[1]] | ||||||||||
|
||||||||||
for row_index in range(1, num_rows): | ||||||||||
temp_row = [0] + result[-1] + [0] | ||||||||||
row_length = row_index + 1 | ||||||||||
|
||||||||||
# It calculates the number of distinct elements in a row | ||||||||||
distinct_elements = ( | ||||||||||
row_length // 2 if row_length % 2 == 0 else row_length // 2 + 1 | ||||||||||
) | ||||||||||
|
||||||||||
row_first_half = [ | ||||||||||
temp_row[x - 1] + temp_row[x] for x in range(1, distinct_elements + 1) | ||||||||||
] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Suggested change
|
||||||||||
|
||||||||||
row_second_half = row_first_half[: (row_index + 1) // 2] | ||||||||||
row_second_half.reverse() | ||||||||||
row = row_first_half + row_second_half | ||||||||||
result.append(row) | ||||||||||
|
||||||||||
return result | ||||||||||
|
||||||||||
|
||||||||||
if __name__ == "__main__": | ||||||||||
import doctest | ||||||||||
|
||||||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leverage the Python builtin functions: