-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Conversation
…ed function to generate pascal's triangle to 'pascal_triangle.py' program. Added some aadditional doctests to the existing function. Added some type check functionality to the existing function.
for more information, see https://pre-commit.ci
Cool! Please add a timeit (or similar) benchmark to prove that the proposed code is faster than the original code. There are lots of examples in this repo. https://github.com/TheAlgorithms/Python/search?q=timeit but https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/count_number_of_one_bits.py#L63 is probably one of the best. |
other/pascal_triangle.py
Outdated
distinct_elements = ( | ||
row_length // 2 if row_length % 2 == 0 else row_length // 2 + 1 | ||
) |
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:
distinct_elements = ( | |
row_length // 2 if row_length % 2 == 0 else row_length // 2 + 1 | |
) | |
distinct_elements = sum(divmod(row_length, 2)) |
other/pascal_triangle.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use i
for integers, indexes, etc. Use item
for objects in a list. Use x
only when space is tight.
row_first_half = [ | |
temp_row[x - 1] + temp_row[x] for x in range(1, distinct_elements + 1) | |
] | |
row_first_half = [item + temp_row[i + 1] for i, item in enumerate(temp_row)] |
The benchmarks show that for some values of |
Describe your change: Added a new optimized function 'generate_pascal_triangle_optimized' to generate Pascal's Triangle to the existing 'pascal_triangle.py' file which reduces the computations done for each row by half. It reduces the redundant computations in generating each row. Also, added type check functionalities, which raise 'ValueError' and 'TypeError', to the existing 'generate_pascal_triangle' function. Also, added 3 additional doctests for the existing 'generate_pascal_triangle' function'.
Checklist:
Fixes: #{$ISSUE_NO}
.