Skip to content

Commit 58744f3

Browse files
* docs: kata description * kata/count-the-ones --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent df9b1ec commit 58744f3

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

kata/7-kyu/count-the-ones/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# [Count the Ones](https://www.codewars.com/kata/count-the-ones "https://www.codewars.com/kata/5519e930cd82ff8a9a000216")
2+
3+
The __Hamming weight__ of a string is the number of symbols that are different from the zero-symbol of the alphabet used. There are several
4+
algorithms for efficient computing of the Hamming weight for numbers. In this Kata, speaking technically, you have to find out the number
5+
of '1' bits in a binary representation of a number. Thus,
6+
7+
```
8+
hammingWeight(10) // 1010 => 2
9+
hammingWeight(21) // 10101 => 3
10+
```
11+
12+
The interesting part of this task is that you have to do it *without* string operation (hey, it's not really interesting otherwise)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface HammingWeight {
2+
static int hammingWeight(int i) {
3+
return Integer.bitCount(i);
4+
}
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
10, 2
10+
21, 3
11+
""")
12+
void sample(int n, int expected) {
13+
assertEquals(expected, HammingWeight.hammingWeight(n));
14+
}
15+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
- [Count consonants](count-consonants "564e7fc20f0b53eb02000106")
120120
- [Count the Digit](count-the-digit "566fc12495810954b1000030")
121121
- [Count the divisors of a number](count-the-divisors-of-a-number "542c0f198e077084c0000c2e")
122+
- [Count the Ones](count-the-ones "5519e930cd82ff8a9a000216")
122123
- [Covfefe](covfefe "592fd8f752ee71ac7e00008a")
123124
- [Create palindrome](create-palindrome "5b7bd90ef643c4df7400015d")
124125
- [Credit card issuer checking](credit-card-issuer-checking "5701e43f86306a615c001868")

0 commit comments

Comments
 (0)