Skip to content

Commit 25ffdd9

Browse files
committed
1 parent 4155591 commit 25ffdd9

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
# [Count the Ones](https://www.codewars.com/kata/count-the-ones "https://www.codewars.com/kata/5519e930cd82ff8a9a000216")
22

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 algorithms for efficient computing of the Hamming weight for numbers. In this Kata, speaking technically, you have to find out the number of '1' bits in a binary representation of a number. Thus,
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,
46

5-
```ruby
6-
hamming_weight(10) # 1010 => 2
7-
hamming_weight(21) # 10101 => 3
87
```
9-
```javascript
108
hammingWeight(10) // 1010 => 2
119
hammingWeight(21) // 10101 => 3
1210
```
1311

14-
The interesting part of this task is that you have to do it *without* string operation (hey, it's not really interesting otherwise)
15-
16-
;)
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+
}

0 commit comments

Comments
 (0)