Skip to content

Commit 63739f4

Browse files
authored
refactor: simplify HammingDistance (TheAlgorithms#4218)
* refactor: make HammingDistance an utility class * tests: add some tests, simplify logic of some * refator: simplify logic in HammingDistance * style: remove logging messages
1 parent 7a3273a commit 63739f4

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

src/main/java/com/thealgorithms/others/cn/HammingDistance.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class HammingDistance {
6+
final public class HammingDistance {
7+
private HammingDistance() {
8+
}
79

8-
public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
9-
if (senderBits.length() != receiverBits.length()) {
10-
throw new IllegalArgumentException("Sender and Receiver bits should be same");
10+
private static void checkChar(char inChar) {
11+
if (inChar != '0' && inChar != '1') {
12+
throw new IllegalArgumentException("Input must be a binary string.");
1113
}
14+
}
1215

13-
List<byte[]> byteArray = new ArrayList<>();
14-
15-
byteArray.add(senderBits.getBytes());
16-
byteArray.add(receiverBits.getBytes());
16+
public static int compute(char charA, char charB) {
17+
checkChar(charA);
18+
checkChar(charB);
19+
return charA == charB ? 0 : 1;
20+
}
1721

18-
byte[] senderData = byteArray.get(0);
19-
byte[] receiverData = byteArray.get(1);
22+
public static int compute(String bitsStrA, String bitsStrB) {
23+
if (bitsStrA.length() != bitsStrB.length()) {
24+
throw new IllegalArgumentException("Input strings must have the same length.");
25+
}
2026

2127
int totalErrorBitCount = 0;
2228

23-
for (int i = 0; i < senderData.length; i++) {
24-
totalErrorBitCount += senderData[i] ^ receiverData[i];
29+
for (int i = 0; i < bitsStrA.length(); i++) {
30+
totalErrorBitCount += compute(bitsStrA.charAt(i), bitsStrB.charAt(i));
2531
}
2632

27-
if (totalErrorBitCount == 0) {
28-
System.out.println("No Error bit in data segments");
29-
} else {
30-
System.out.println("Total Error bit count " + totalErrorBitCount);
31-
}
3233
return totalErrorBitCount;
3334
}
3435
}

src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@
66
import org.junit.jupiter.api.Test;
77

88
public class HammingDistanceTest {
9-
10-
HammingDistance hd;
11-
12-
@BeforeEach
13-
void initialize() {
14-
hd = new HammingDistance();
15-
}
16-
179
@Test
1810
public void checkForDifferentBits() {
19-
String senderBits = "000", receiverBits = "011";
20-
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
11+
int answer = HammingDistance.compute("000", "011");
2112
Assertions.assertThat(answer).isEqualTo(2);
2213
}
2314

@@ -32,38 +23,62 @@ public void checkForDifferentBits() {
3223
*/
3324
@Test
3425
public void checkForDifferentBitsLength() {
35-
String senderBits = "10101", receiverBits = "11110";
36-
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
26+
int answer = HammingDistance.compute("10101", "11110");
3727
Assertions.assertThat(answer).isEqualTo(3);
3828
}
3929

4030
@Test
4131
public void checkForSameBits() {
42-
String senderBits = "111", receiverBits = "111";
43-
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
32+
String someBits = "111";
33+
int answer = HammingDistance.compute(someBits, someBits);
4434
Assertions.assertThat(answer).isEqualTo(0);
4535
}
4636

4737
@Test
4838
public void checkForLongDataBits() {
49-
String senderBits = "10010101101010000100110100", receiverBits = "00110100001011001100110101";
50-
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
39+
int answer = HammingDistance.compute("10010101101010000100110100", "00110100001011001100110101");
5140
Assertions.assertThat(answer).isEqualTo(7);
5241
}
5342

5443
@Test
5544
public void mismatchDataBits() {
56-
String senderBits = "100010", receiverBits = "00011";
45+
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("100010", "00011"); });
46+
47+
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
48+
}
5749

58-
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits); });
50+
@Test
51+
public void mismatchDataBits2() {
52+
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1", "11"); });
5953

60-
Assertions.assertThat(ex.getMessage()).contains("bits should be same");
54+
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
6155
}
6256

6357
@Test
6458
public void checkForLongDataBitsSame() {
65-
String senderBits = "10010101101010000100110100", receiverBits = "10010101101010000100110100";
66-
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
59+
String someBits = "10010101101010000100110100";
60+
int answer = HammingDistance.compute(someBits, someBits);
6761
Assertions.assertThat(answer).isEqualTo(0);
6862
}
63+
64+
@Test
65+
public void checkForEmptyInput() {
66+
String someBits = "";
67+
int answer = HammingDistance.compute(someBits, someBits);
68+
Assertions.assertThat(answer).isEqualTo(0);
69+
}
70+
71+
@Test
72+
public void checkForInputOfLength1() {
73+
String someBits = "0";
74+
int answer = HammingDistance.compute(someBits, someBits);
75+
Assertions.assertThat(answer).isEqualTo(0);
76+
}
77+
78+
@Test
79+
public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
80+
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1A", "11"); });
81+
82+
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
83+
}
6984
}

0 commit comments

Comments
 (0)