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
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Bit-Manipulation/test/CountSetBits.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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.


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)
})

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