From 5f3116b54cbdf30d98b80e0204e98987e44ff68d Mon Sep 17 00:00:00 2001 From: Valery Malyshev Date: Sat, 15 Jun 2024 16:04:59 +0300 Subject: [PATCH] Bit Counting --- 6_kyu/Bit Counting/README.md | 7 +++++++ 6_kyu/Bit Counting/index.test.ts | 12 ++++++++++++ 6_kyu/Bit Counting/index.ts | 8 ++++++++ README.md | 4 ++-- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 6_kyu/Bit Counting/README.md create mode 100644 6_kyu/Bit Counting/index.test.ts create mode 100644 6_kyu/Bit Counting/index.ts diff --git a/6_kyu/Bit Counting/README.md b/6_kyu/Bit Counting/README.md new file mode 100644 index 0000000..c243edf --- /dev/null +++ b/6_kyu/Bit Counting/README.md @@ -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 diff --git a/6_kyu/Bit Counting/index.test.ts b/6_kyu/Bit Counting/index.test.ts new file mode 100644 index 0000000..82837cb --- /dev/null +++ b/6_kyu/Bit Counting/index.test.ts @@ -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); + }); +}); diff --git a/6_kyu/Bit Counting/index.ts b/6_kyu/Bit Counting/index.ts new file mode 100644 index 0000000..d408802 --- /dev/null +++ b/6_kyu/Bit Counting/index.ts @@ -0,0 +1,8 @@ +export function countBits(n: number): number { + const numbers = n + .toString(2) + .split("") + .filter((n) => n === "1"); + + return numbers.length; +} diff --git a/README.md b/README.md index a33c9fa..cb2d3f2 100644 --- a/README.md +++ b/README.md @@ -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 \