Skip to content

Commit

Permalink
Bit Counting
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed Jun 15, 2024
1 parent 85ea20b commit 5f3116b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions 6_kyu/Bit Counting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Bit Counting

https://www.codewars.com/kata/526571aae218b8ee490006f4

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example: The binary representation of `1234` is `10011010010`, so the function should return 5 in this case
12 changes: 12 additions & 0 deletions 6_kyu/Bit Counting/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { countBits } from "../../7_kyu/_example";

describe("Tests", () => {
it("example", () => {
expect(countBits(1234)).toBe(5);
expect(countBits(0)).toBe(0);
expect(countBits(4)).toBe(1);
expect(countBits(7)).toBe(3);
expect(countBits(9)).toBe(2);
expect(countBits(10)).toBe(2);
});
});
8 changes: 8 additions & 0 deletions 6_kyu/Bit Counting/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function countBits(n: number): number {
const numbers = n
.toString(2)
.split("")
.filter((n) => n === "1");

return numbers.length;
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

### Katas solved

`Total`: 106 \
`Total`: 107 \
`8_kyu`: 87 \
`7_kyu`: 14 \
`6_kyu`: 5 \
`6_kyu`: 6 \
`5_kyu`: 0 \
`4_kyu`: 0 \
`3_kyu`: 0 \
Expand Down

0 comments on commit 5f3116b

Please sign in to comment.