Skip to content

Commit 2fbb1d6

Browse files
author
Mihir Sawant
authored
Add hamming distance (TheAlgorithms#3270)
1 parent 07a5531 commit 2fbb1d6

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.others.cn;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class HammingDistance {
7+
8+
9+
10+
public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
11+
12+
if(senderBits.length() != receiverBits.length()) {
13+
throw new IllegalArgumentException("Sender and Receiver bits should be same");
14+
}
15+
16+
List<byte[]> byteArray = new ArrayList<>();
17+
18+
byteArray.add(senderBits.getBytes());
19+
byteArray.add(receiverBits.getBytes());
20+
21+
22+
byte[] senderData = byteArray.get(0);
23+
byte[] receiverData = byteArray.get(1);
24+
25+
int totalErrorBitCount = 0;
26+
27+
for(int i = 0; i < senderData.length; i++){
28+
totalErrorBitCount += senderData[i] ^ receiverData[i];
29+
}
30+
31+
if(totalErrorBitCount == 0){
32+
System.out.println("No Error bit in data segments");
33+
} else{
34+
System.out.println("Total Error bit count "+totalErrorBitCount);
35+
}
36+
return totalErrorBitCount;
37+
}
38+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.others.cn;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.assertj.core.api.ThrowableTypeAssert;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
9+
public class HammingDistanceTest {
10+
11+
HammingDistance hd;
12+
13+
@BeforeEach
14+
void initialize(){
15+
hd = new HammingDistance();
16+
}
17+
18+
@Test
19+
public void checkForDifferentBits(){
20+
String senderBits = "000", receiverBits = "011";
21+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
22+
Assertions.assertThat(answer).isEqualTo(2);
23+
}
24+
/*
25+
26+
1 0 1 0 1
27+
1 1 1 1 0
28+
----------
29+
0 1 0 1 1
30+
31+
32+
*/
33+
@Test
34+
public void checkForDifferentBitsLength(){
35+
String senderBits = "10101", receiverBits = "11110";
36+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
37+
Assertions.assertThat(answer).isEqualTo(3);
38+
}
39+
40+
41+
@Test
42+
public void checkForSameBits(){
43+
String senderBits = "111", receiverBits = "111";
44+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
45+
Assertions.assertThat(answer).isEqualTo(0);
46+
}
47+
48+
@Test
49+
public void checkForLongDataBits(){
50+
String senderBits = "10010101101010000100110100", receiverBits = "00110100001011001100110101";
51+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
52+
Assertions.assertThat(answer).isEqualTo(7);
53+
}
54+
55+
@Test
56+
public void mismatchDataBits(){
57+
String senderBits = "100010", receiverBits = "00011";
58+
59+
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () ->{
60+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
61+
});
62+
63+
Assertions.assertThat(ex.getMessage()).contains("bits should be same");
64+
65+
}
66+
67+
@Test
68+
public void checkForLongDataBitsSame(){
69+
String senderBits = "10010101101010000100110100", receiverBits = "10010101101010000100110100";
70+
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
71+
Assertions.assertThat(answer).isEqualTo(0);
72+
}
73+
74+
75+
76+
}

0 commit comments

Comments
 (0)