Skip to content

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 6 commits into from
Oct 31, 2022
Merged

Modified 'pascal_triangle.py' program #7901

merged 6 commits into from
Oct 31, 2022

Conversation

TechFreak107
Copy link
Contributor

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'.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

…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.
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Oct 31, 2022
@cclauss
Copy link
Member

cclauss commented Oct 31, 2022

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.

Comment on lines 158 to 160
distinct_elements = (
row_length // 2 if row_length % 2 == 0 else row_length // 2 + 1
)
Copy link
Member

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:

Suggested change
distinct_elements = (
row_length // 2 if row_length % 2 == 0 else row_length // 2 + 1
)
distinct_elements = sum(divmod(row_length, 2))

Comment on lines 162 to 164
row_first_half = [
temp_row[x - 1] + temp_row[x] for x in range(1, distinct_elements + 1)
]
Copy link
Member

@cclauss cclauss Oct 31, 2022

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.

Suggested change
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)]

@cclauss
Copy link
Member

cclauss commented Oct 31, 2022

The benchmarks show that for some values of num_rows, the optimized version is slower and for other values, it is faster.

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 31, 2022
@cclauss cclauss merged commit fecbf59 into TheAlgorithms:master Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants