Add hamming distance (#3270)

This commit is contained in:
Mihir Sawant 2022-09-24 18:44:39 +05:30 committed by GitHub
parent 07a5531f1a
commit 2fbb1d6402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.thealgorithms.others.cn;
import java.util.ArrayList;
import java.util.List;
public class HammingDistance {
public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
if(senderBits.length() != receiverBits.length()) {
throw new IllegalArgumentException("Sender and Receiver bits should be same");
}
List<byte[]> byteArray = new ArrayList<>();
byteArray.add(senderBits.getBytes());
byteArray.add(receiverBits.getBytes());
byte[] senderData = byteArray.get(0);
byte[] receiverData = byteArray.get(1);
int totalErrorBitCount = 0;
for(int i = 0; i < senderData.length; i++){
totalErrorBitCount += senderData[i] ^ receiverData[i];
}
if(totalErrorBitCount == 0){
System.out.println("No Error bit in data segments");
} else{
System.out.println("Total Error bit count "+totalErrorBitCount);
}
return totalErrorBitCount;
}
}

View File

@ -0,0 +1,76 @@
package com.thealgorithms.others.cn;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableTypeAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class HammingDistanceTest {
HammingDistance hd;
@BeforeEach
void initialize(){
hd = new HammingDistance();
}
@Test
public void checkForDifferentBits(){
String senderBits = "000", receiverBits = "011";
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
Assertions.assertThat(answer).isEqualTo(2);
}
/*
1 0 1 0 1
1 1 1 1 0
----------
0 1 0 1 1
*/
@Test
public void checkForDifferentBitsLength(){
String senderBits = "10101", receiverBits = "11110";
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
Assertions.assertThat(answer).isEqualTo(3);
}
@Test
public void checkForSameBits(){
String senderBits = "111", receiverBits = "111";
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForLongDataBits(){
String senderBits = "10010101101010000100110100", receiverBits = "00110100001011001100110101";
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
Assertions.assertThat(answer).isEqualTo(7);
}
@Test
public void mismatchDataBits(){
String senderBits = "100010", receiverBits = "00011";
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () ->{
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
});
Assertions.assertThat(ex.getMessage()).contains("bits should be same");
}
@Test
public void checkForLongDataBitsSame(){
String senderBits = "10010101101010000100110100", receiverBits = "10010101101010000100110100";
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
Assertions.assertThat(answer).isEqualTo(0);
}
}