-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from 2 commits
b749625
a6aebb4
ba6f851
d0ba2a3
474cc9a
aa4dd7c
d90363b
9ec4f5f
fe9555b
c0da047
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { CountSetBits } from '../CountSetBits' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) }) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not use quoted for keys because of: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| test('check CountSetBits of 25 is 3', () => { | ||
| const res = CountSetBits(25) | ||
| expect(res).toBe(3) | ||
| }) | ||
| test('check CountSetBits of 36 is 2', () => { | ||
| const res = CountSetBits(36) | ||
| expect(res).toBe(2) | ||
| }) | ||
| test('check CountSetBits of 16 is 1', () => { | ||
| const res = CountSetBits(16) | ||
| expect(res).toBe(1) | ||
| }) | ||
| test('check CountSetBits of 58 is 4', () => { | ||
| const res = CountSetBits(58) | ||
| expect(res).toBe(4) | ||
| }) | ||
| test('check CountSetBits of 0 is 0', () => { | ||
| const res = CountSetBits(0) | ||
| expect(res).toBe(0) | ||
| }) | ||