Skip to content

Update count_number_of_one_bits.py #7589

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 10 commits into from
Oct 24, 2022
Merged

Conversation

JatinR05
Copy link
Contributor

removed the modulo operator as it is very time consuming in comparison to the and operator

Describe your change:

  • 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 have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

removed the modulo operator as it is very time consuming in comparison to the and operator
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Oct 24, 2022
@cclauss
Copy link
Member

cclauss commented Oct 24, 2022

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

JatinR05 and others added 2 commits October 24, 2022 13:28
Updated with the timeit library to compare. Moreover I have updated my code which helps us in reaching the output comparatively faster.
@JatinR05
Copy link
Contributor Author

I have updated my code. It would be great if you could have a look into it.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 24, 2022
@JatinR05
Copy link
Contributor Author

I have updated the code as suggested.

@cclauss
Copy link
Member

cclauss commented Oct 24, 2022

Please run this on your own machine. The doctest fails.

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 24, 2022
@JatinR05
Copy link
Contributor Author

I am extremely sorry for the silly mistakes. Please could you check it now

@cclauss
Copy link
Member

cclauss commented Oct 24, 2022

Would it be more self-documenting to change the variable number to bit_length throughout the script?

Let's make benchmark() more readable with something like this.

def benchmark() -> None:
    """
    Benchmark code for comparing 2 functions, with different length int values.
    """
    def do_benchmark(number: int) -> None:
        setup = "import __main__ as z"
        print(f"Benchmark when {number = }:")
        print(f"{get_set_bits_count_using_modulo_operator(number) = }")
        timing = timeit("z.get_set_bits_count_using_modulo_operator(25)", setup=setup)
        print(f"timeit() runs in {timing} seconds")
        print(f"{get_set_bits_count_using_brian_kernighans_algorithm(number) = }")
        timing = timeit(
            "z.get_set_bits_count_using_brian_kernighans_algorithm(25)",
            setup=setup,
        )
        print(f"timeit() runs in {timing} seconds")

    for number in (25, 37, 58, 0):
        do_benchmark(number)
        print()

@JatinR05
Copy link
Contributor Author

I guess the variable name "number" is more appropriate as it's representing the number which the user will be providing as the input and not the bit_length.
Regarding the benchmark updates, it's really a great idea. I'll surely update it.

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 24, 2022
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome contribution. Thanks for doing this!!

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting reviews This PR is ready to be reviewed labels Oct 24, 2022
@cclauss cclauss merged commit bb07854 into TheAlgorithms:master Oct 24, 2022
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 24, 2022
@JatinR05
Copy link
Contributor Author

Hi could you please add a label of hacktoberfest-accepted.

@cclauss cclauss added hacktoberfest hacktoberfest-accepted Accepted to be counted towards Hacktoberfest labels Oct 24, 2022
@JatinR05
Copy link
Contributor Author

Thank you.

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 hacktoberfest hacktoberfest-accepted Accepted to be counted towards Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants