From d21c7a5ba43c051baa945675b7877d90a393504c Mon Sep 17 00:00:00 2001 From: ligumas Date: Wed, 22 Apr 2026 08:28:05 +0000 Subject: [PATCH] Master ###Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request. 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. (Note: this PR modifies documentation and adds a test file.) 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 description above includes the issue number(s) with a closing keyword (e.g. Fixes #123). Short summary: Added a short usage example to README.md and a simple unit test test_count_number_of_one_bits.py. What changed and why: What: Added an example and a small unittest for the set-bits functions; did not change algorithm logic in count_number_of_one_bits.py. The main function shown is get_set_bits_count_using_brian_kernighans_algorithm. Why: Improve discoverability and add a lightweight regression test to catch future breakages. Impact: Documentation + test only; no functional changes. --- bit_manipulation/README.md | 19 +++++++++++++++++++ tests/test_count_number_of_one_bits.py | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/test_count_number_of_one_bits.py diff --git a/bit_manipulation/README.md b/bit_manipulation/README.md index 3f5e028beb8e..cb332f42ac8d 100644 --- a/bit_manipulation/README.md +++ b/bit_manipulation/README.md @@ -9,3 +9,22 @@ Bit manipulation is the act of manipulating bits to detect errors (hamming code) * * * + +## Example + +Below is a simple example using the `get_set_bits_count_using_brian_kernighans_algorithm` +function from [bit_manipulation/count_number_of_one_bits.py](bit_manipulation/count_number_of_one_bits.py). + +```python +from bit_manipulation.count_number_of_one_bits import get_set_bits_count_using_brian_kernighans_algorithm + +print(get_set_bits_count_using_brian_kernighans_algorithm(25)) # 3 +print(get_set_bits_count_using_brian_kernighans_algorithm(58)) # 4 +``` + +This repository also includes doctest examples in the implementation that can be run with: + +```bash +python3 bit_manipulation/count_number_of_one_bits.py +``` + diff --git a/tests/test_count_number_of_one_bits.py b/tests/test_count_number_of_one_bits.py new file mode 100644 index 000000000000..d164e0e75157 --- /dev/null +++ b/tests/test_count_number_of_one_bits.py @@ -0,0 +1,22 @@ +import unittest + +from bit_manipulation.count_number_of_one_bits import ( + get_set_bits_count_using_brian_kernighans_algorithm, + get_set_bits_count_using_modulo_operator, +) + + +class TestCountSetBits(unittest.TestCase): + def test_examples(self): + self.assertEqual(get_set_bits_count_using_brian_kernighans_algorithm(25), 3) + self.assertEqual(get_set_bits_count_using_brian_kernighans_algorithm(58), 4) + self.assertEqual(get_set_bits_count_using_modulo_operator(37), 3) + self.assertEqual(get_set_bits_count_using_modulo_operator(0), 0) + + def test_negative_input_raises(self): + with self.assertRaises(ValueError): + get_set_bits_count_using_brian_kernighans_algorithm(-1) + + +if __name__ == "__main__": + unittest.main()