Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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.
Please fix your code style.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There are still unresolved review comments. |
@@ -0,0 +1,22 @@ | |||
import { CountSetBits } from '../CountSetBits' |
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.
I was more thinking something like:
test("CountSetBits",()=>{consttests={// binary representation: set bits'1001':2,'111':3,}for(binaryintests)expect(CountSetBits(parseInt(binary,2))).toBe(tests[binary])})
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.
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.
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:
consttestCountSetBits=(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:
consttestCountSetBits=(input_bin_str,expected_output)=>expect(CountSetBits(parseInt(input,2))).toBe(expected_output)test('CountSetBits',()=>{testCountSetBits("0",0)testCountSetBits("10",1)})
etc.
expect(res).toBe(0) | ||
test('CountSetBits', () => { | ||
const tests = { | ||
// binary representation: set bits |
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.
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 toCountSetBits
.
test('CountSetBits', () => { | ||
const tests = { | ||
// binary representation: set bits |
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.
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.
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.
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.
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.
https://iq.opengenus.org/brian-kernighan-algorithm/
The algorithm accepts an integer as given in the algorithm.
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.
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 intoCountSetBits
, JS will coerce them back to numbers as soon as you start doing arithmetic & bit ops on them.
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. |
Describe your change:
Checklist:
Example:
UserProfile.js
is allowed butuserprofile.js
,Userprofile.js
,user-Profile.js
,userProfile.js
are notFixes: #{$ISSUE_NO}
.