refactor: simplify HammingDistance
(#4218)
* refactor: make HammingDistance an utility class * tests: add some tests, simplify logic of some * refator: simplify logic in HammingDistance * style: remove logging messages
This commit is contained in:
parent
7a3273ae1d
commit
63739f4933
@ -3,32 +3,33 @@ package com.thealgorithms.others.cn;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class HammingDistance {
|
final public class HammingDistance {
|
||||||
|
private HammingDistance() {
|
||||||
|
}
|
||||||
|
|
||||||
public int getHammingDistanceBetweenBits(String senderBits, String receiverBits) {
|
private static void checkChar(char inChar) {
|
||||||
if (senderBits.length() != receiverBits.length()) {
|
if (inChar != '0' && inChar != '1') {
|
||||||
throw new IllegalArgumentException("Sender and Receiver bits should be same");
|
throw new IllegalArgumentException("Input must be a binary string.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<byte[]> byteArray = new ArrayList<>();
|
public static int compute(char charA, char charB) {
|
||||||
|
checkChar(charA);
|
||||||
|
checkChar(charB);
|
||||||
|
return charA == charB ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
byteArray.add(senderBits.getBytes());
|
public static int compute(String bitsStrA, String bitsStrB) {
|
||||||
byteArray.add(receiverBits.getBytes());
|
if (bitsStrA.length() != bitsStrB.length()) {
|
||||||
|
throw new IllegalArgumentException("Input strings must have the same length.");
|
||||||
byte[] senderData = byteArray.get(0);
|
}
|
||||||
byte[] receiverData = byteArray.get(1);
|
|
||||||
|
|
||||||
int totalErrorBitCount = 0;
|
int totalErrorBitCount = 0;
|
||||||
|
|
||||||
for (int i = 0; i < senderData.length; i++) {
|
for (int i = 0; i < bitsStrA.length(); i++) {
|
||||||
totalErrorBitCount += senderData[i] ^ receiverData[i];
|
totalErrorBitCount += compute(bitsStrA.charAt(i), bitsStrB.charAt(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (totalErrorBitCount == 0) {
|
|
||||||
System.out.println("No Error bit in data segments");
|
|
||||||
} else {
|
|
||||||
System.out.println("Total Error bit count " + totalErrorBitCount);
|
|
||||||
}
|
|
||||||
return totalErrorBitCount;
|
return totalErrorBitCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,9 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class HammingDistanceTest {
|
public class HammingDistanceTest {
|
||||||
|
|
||||||
HammingDistance hd;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void initialize() {
|
|
||||||
hd = new HammingDistance();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkForDifferentBits() {
|
public void checkForDifferentBits() {
|
||||||
String senderBits = "000", receiverBits = "011";
|
int answer = HammingDistance.compute("000", "011");
|
||||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
|
||||||
Assertions.assertThat(answer).isEqualTo(2);
|
Assertions.assertThat(answer).isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,38 +23,62 @@ public class HammingDistanceTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void checkForDifferentBitsLength() {
|
public void checkForDifferentBitsLength() {
|
||||||
String senderBits = "10101", receiverBits = "11110";
|
int answer = HammingDistance.compute("10101", "11110");
|
||||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
|
||||||
Assertions.assertThat(answer).isEqualTo(3);
|
Assertions.assertThat(answer).isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkForSameBits() {
|
public void checkForSameBits() {
|
||||||
String senderBits = "111", receiverBits = "111";
|
String someBits = "111";
|
||||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
int answer = HammingDistance.compute(someBits, someBits);
|
||||||
Assertions.assertThat(answer).isEqualTo(0);
|
Assertions.assertThat(answer).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkForLongDataBits() {
|
public void checkForLongDataBits() {
|
||||||
String senderBits = "10010101101010000100110100", receiverBits = "00110100001011001100110101";
|
int answer = HammingDistance.compute("10010101101010000100110100", "00110100001011001100110101");
|
||||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
|
||||||
Assertions.assertThat(answer).isEqualTo(7);
|
Assertions.assertThat(answer).isEqualTo(7);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mismatchDataBits() {
|
public void mismatchDataBits() {
|
||||||
String senderBits = "100010", receiverBits = "00011";
|
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("100010", "00011"); });
|
||||||
|
|
||||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits); });
|
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||||
|
}
|
||||||
|
|
||||||
Assertions.assertThat(ex.getMessage()).contains("bits should be same");
|
@Test
|
||||||
|
public void mismatchDataBits2() {
|
||||||
|
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1", "11"); });
|
||||||
|
|
||||||
|
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkForLongDataBitsSame() {
|
public void checkForLongDataBitsSame() {
|
||||||
String senderBits = "10010101101010000100110100", receiverBits = "10010101101010000100110100";
|
String someBits = "10010101101010000100110100";
|
||||||
int answer = hd.getHammingDistanceBetweenBits(senderBits, receiverBits);
|
int answer = HammingDistance.compute(someBits, someBits);
|
||||||
Assertions.assertThat(answer).isEqualTo(0);
|
Assertions.assertThat(answer).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkForEmptyInput() {
|
||||||
|
String someBits = "";
|
||||||
|
int answer = HammingDistance.compute(someBits, someBits);
|
||||||
|
Assertions.assertThat(answer).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkForInputOfLength1() {
|
||||||
|
String someBits = "0";
|
||||||
|
int answer = HammingDistance.compute(someBits, someBits);
|
||||||
|
Assertions.assertThat(answer).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
|
||||||
|
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1A", "11"); });
|
||||||
|
|
||||||
|
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user