Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Kernighan's Algorithm for Counting Set Bits #1107

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

Open
smitgabani wants to merge 10 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from smitgabani:master

Conversation

@smitgabani
Copy link

@smitgabani smitgabani commented Sep 23, 2022

Open in Gitpod know more

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 JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • 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}.

Copy link
Collaborator

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

Please fix your code style.

@appgurueu appgurueu changed the title (削除) fixes #1106 adding CountSetBits algorithm (削除ここまで) (追記) Add Kernighan's Algorithm for Counting Set Bits (追記ここまで) Sep 23, 2022
Copy link
Collaborator

There are still unresolved review comments.

@@ -0,0 +1,22 @@
import { CountSetBits } from '../CountSetBits'
Copy link
Collaborator

@appgurueu appgurueu Sep 26, 2022

Choose a reason for hiding this comment

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

I was more thinking something like:

test("CountSetBits", () => {
 const tests = {
 // binary representation: set bits
 '1001': 2,
 '111': 3,
 }
 for (binary in tests)
 expect(CountSetBits(parseInt(binary, 2))).toBe(tests[binary])
})

Copy link
Author

@smitgabani smitgabani Sep 26, 2022

Choose a reason for hiding this comment

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

I did not use quoted for keys because of:
image

Copy link
Collaborator

@appgurueu appgurueu Sep 27, 2022

Choose a reason for hiding this comment

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

I did not intend a reversal to the more verbose form. You can even use a list of lists if you like - all you need is a convenient way to test that a certain input leads to a certain output without copy-pasting. Here's another alternative:

const testCountSetBits = (input, expected_output) => expect(CountSetBits(input)).toBe(expected_output)
test('CountSetBits', () => {
 testCountSetBits(25, 3)
 testCountSetBits(36, 2)
})

etc., or, IMO preferable since it makes the binary representation apparent:

const testCountSetBits = (input_bin_str, expected_output) => expect(CountSetBits(parseInt(input, 2))).toBe(expected_output)
test('CountSetBits', () => {
 testCountSetBits("0", 0)
 testCountSetBits("10", 1)
})

etc.

Copy link
Author

I did not use quotes because of npm run style test
image

expect(res).toBe(0)
test('CountSetBits', () => {
const tests = {
// binary representation: set bits
Copy link
Collaborator

@appgurueu appgurueu Sep 26, 2022

Choose a reason for hiding this comment

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

This is not accurate anymore - now this is a decimal representation. I also don't quite like this, since it relies on back-and-forth coercion of numbers to strings and back to numbers when passing them to CountSetBits.


test('CountSetBits', () => {
const tests = {
// binary representation: set bits
Copy link
Author

@smitgabani smitgabani Sep 26, 2022

Choose a reason for hiding this comment

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

The algorithm accepts decimal representation and counts the bits in its binary representation. The comment binary representation:set bits in not correct and I will remove it in the next commit.

Copy link
Author

@smitgabani smitgabani Sep 26, 2022

Choose a reason for hiding this comment

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

I have changed the comment it should be a decimal representation to set bits. Counting the number of set bits in a binary representation is only counting the number of ones and the algorithm does not do that. It counts bit sets in the decimal representation of the number. The tests and algorithm deal with numbers only and not strings.

Copy link
Author

@smitgabani smitgabani Sep 26, 2022

Choose a reason for hiding this comment

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

https://iq.opengenus.org/brian-kernighan-algorithm/
The algorithm accepts an integer as given in the algorithm.

Copy link
Collaborator

@appgurueu appgurueu Sep 26, 2022

Choose a reason for hiding this comment

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

It counts bit sets in the decimal representation of the number.

A decimal representation does not have a notion of bits. A decimal representation has digits from 0 - 9.

The tests and algorithm deal with numbers only and not strings.

I'm aware. But using numbers as keys for a JS object will coerce them to strings. When passing the numbers into CountSetBits, JS will coerce them back to numbers as soon as you start doing arithmetic & bit ops on them.

Copy link

stale bot commented Nov 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Closed due to age label Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@raklaptudirm raklaptudirm Awaiting requested review from raklaptudirm raklaptudirm is a code owner

@appgurueu appgurueu Awaiting requested review from appgurueu appgurueu is a code owner

Requested changes must be addressed to merge this pull request.

Assignees

No one assigned

Labels

stale Closed due to age

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /