mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
refactor: clean up duplicate algorithm implementations to reduce maintenance overhead (#7256)
refactor: remove duplicate algorithm implementations Removed the following duplicate implementations: - searches/PerfectBinarySearch.java (duplicate of IterativeBinarySearch) - searches/SortOrderAgnosticBinarySearch.java (duplicate of OrderAgnosticBinarySearch) - strings/LongestPalindromicSubstring.java (duplicate of dynamicprogramming version) - strings/ValidParentheses.java (duplicate of stacks version) - others/cn/HammingDistance.java (duplicate - strings version handles text) - others/NewManShanksPrimeTest.java (orphan test in wrong package) Updated DIRECTORY.md to reflect the changes. Fixes #7253 Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.thealgorithms.dynamicprogramming.NewManShanksPrime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NewManShanksPrimeTest {
|
||||
|
||||
@Test
|
||||
void testOne() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTwo() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testThree() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(3, 7));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFour() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(4, 17));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFive() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(5, 41));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSix() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(6, 99));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSeven() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(7, 239));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEight() {
|
||||
assertTrue(NewManShanksPrime.nthManShanksPrime(8, 577));
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package com.thealgorithms.others.cn;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HammingDistanceTest {
|
||||
@Test
|
||||
public void checkForDifferentBits() {
|
||||
int answer = HammingDistance.compute("000", "011");
|
||||
Assertions.assertThat(answer).isEqualTo(2);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
1 0 1 0 1
|
||||
1 1 1 1 0
|
||||
----------
|
||||
0 1 0 1 1
|
||||
|
||||
|
||||
*/
|
||||
@Test
|
||||
public void checkForDifferentBitsLength() {
|
||||
int answer = HammingDistance.compute("10101", "11110");
|
||||
Assertions.assertThat(answer).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForSameBits() {
|
||||
String someBits = "111";
|
||||
int answer = HammingDistance.compute(someBits, someBits);
|
||||
Assertions.assertThat(answer).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForLongDataBits() {
|
||||
int answer = HammingDistance.compute("10010101101010000100110100", "00110100001011001100110101");
|
||||
Assertions.assertThat(answer).isEqualTo(7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mismatchDataBits() {
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("100010", "00011"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mismatchDataBits2() {
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1", "11"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkForLongDataBitsSame() {
|
||||
String someBits = "10010101101010000100110100";
|
||||
int answer = HammingDistance.compute(someBits, someBits);
|
||||
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, () -> { HammingDistance.compute("1A", "11"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author D Sunil (https://github.com/sunilnitdgp)
|
||||
* @see PerfectBinarySearch
|
||||
*/
|
||||
public class PerfectBinarySearchTest {
|
||||
|
||||
@Test
|
||||
public void testIntegerBinarySearch() {
|
||||
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
PerfectBinarySearch<Integer> binarySearch = new PerfectBinarySearch<>();
|
||||
|
||||
// Test cases for elements present in the array
|
||||
assertEquals(0, binarySearch.find(array, 1)); // First element
|
||||
assertEquals(4, binarySearch.find(array, 5)); // Middle element
|
||||
assertEquals(9, binarySearch.find(array, 10)); // Last element
|
||||
assertEquals(6, binarySearch.find(array, 7)); // Element in the middle
|
||||
|
||||
// Test cases for elements not in the array
|
||||
assertEquals(-1, binarySearch.find(array, 0)); // Element before the array
|
||||
assertEquals(-1, binarySearch.find(array, 11)); // Element after the array
|
||||
assertEquals(-1, binarySearch.find(array, 100)); // Element not in the array
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringBinarySearch() {
|
||||
String[] array = {"apple", "banana", "cherry", "date", "fig"};
|
||||
PerfectBinarySearch<String> binarySearch = new PerfectBinarySearch<>();
|
||||
|
||||
// Test cases for elements not in the array
|
||||
assertEquals(-1, binarySearch.find(array, "apricot")); // Element not in the array
|
||||
assertEquals(-1, binarySearch.find(array, "bananaa")); // Element not in the array
|
||||
|
||||
// Test cases for elements present in the array
|
||||
assertEquals(0, binarySearch.find(array, "apple")); // First element
|
||||
assertEquals(2, binarySearch.find(array, "cherry")); // Middle element
|
||||
assertEquals(4, binarySearch.find(array, "fig")); // Last element
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SortOrderAgnosticBinarySearchTest {
|
||||
|
||||
@Test
|
||||
public void testAscending() {
|
||||
int[] arr = {1, 2, 3, 4, 5}; // for ascending order.
|
||||
int target = 2;
|
||||
int ans = SortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int excepted = 1;
|
||||
assertEquals(excepted, ans);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescending() {
|
||||
int[] arr = {5, 4, 3, 2, 1}; // for descending order.
|
||||
int target = 2;
|
||||
int ans = SortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int excepted = 3;
|
||||
assertEquals(excepted, ans);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class LongestPalindromicSubstringTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCasesForLongestPalindrome")
|
||||
void testLongestPalindrome(String input, String expected) {
|
||||
assertEquals(expected, LongestPalindromicSubstring.longestPalindrome(input));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCasesForLongestPalindrome() {
|
||||
return Stream.of(Arguments.of("babad", "bab"), Arguments.of("cbbd", "bb"), Arguments.of("a", "a"), Arguments.of("", ""), Arguments.of("abc", "a"), Arguments.of(null, ""), Arguments.of("aaaaa", "aaaaa"));
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
public class ValidParenthesesTest {
|
||||
|
||||
@ParameterizedTest(name = "Input: \"{0}\" → Expected: {1}")
|
||||
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false", "'{{{{}}}}', true", "'[({})]', true", "'[(])', false", "'[', false", "']', false", "'()()()()', true", "'(()', false", "'())', false",
|
||||
"'{[()()]()}', true"})
|
||||
void
|
||||
testIsValid(String input, boolean expected) {
|
||||
assertEquals(expected, ValidParentheses.isValid(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullInputThrows() {
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(null));
|
||||
assertEquals("Input string cannot be null", ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Input: \"{0}\" → throws IllegalArgumentException")
|
||||
@CsvSource({"'a'", "'()a'", "'[123]'", "'{hello}'", "'( )'", "'\t'", "'\n'", "'@#$%'"})
|
||||
void testInvalidCharactersThrow(String input) {
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(input));
|
||||
assertTrue(ex.getMessage().startsWith("Unexpected character"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user