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:
13
DIRECTORY.md
13
DIRECTORY.md
@@ -626,8 +626,6 @@
|
||||
- 📄 [SkylineProblem](src/main/java/com/thealgorithms/others/SkylineProblem.java)
|
||||
- 📄 [TwoPointers](src/main/java/com/thealgorithms/others/TwoPointers.java)
|
||||
- 📄 [Verhoeff](src/main/java/com/thealgorithms/others/Verhoeff.java)
|
||||
- 📁 **cn**
|
||||
- 📄 [HammingDistance](src/main/java/com/thealgorithms/others/cn/HammingDistance.java)
|
||||
- 📁 **physics**
|
||||
- 📄 [CoulombsLaw](src/main/java/com/thealgorithms/physics/CoulombsLaw.java)
|
||||
- 📄 [DampedOscillator](src/main/java/com/thealgorithms/physics/DampedOscillator.java)
|
||||
@@ -701,7 +699,6 @@
|
||||
- 📄 [LowerBound](src/main/java/com/thealgorithms/searches/LowerBound.java)
|
||||
- 📄 [MonteCarloTreeSearch](src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java)
|
||||
- 📄 [OrderAgnosticBinarySearch](src/main/java/com/thealgorithms/searches/OrderAgnosticBinarySearch.java)
|
||||
- 📄 [PerfectBinarySearch](src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)
|
||||
- 📄 [QuickSelect](src/main/java/com/thealgorithms/searches/QuickSelect.java)
|
||||
- 📄 [RabinKarpAlgorithm](src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java)
|
||||
- 📄 [RandomSearch](src/main/java/com/thealgorithms/searches/RandomSearch.java)
|
||||
@@ -710,7 +707,6 @@
|
||||
- 📄 [SaddlebackSearch](src/main/java/com/thealgorithms/searches/SaddlebackSearch.java)
|
||||
- 📄 [SearchInARowAndColWiseSortedMatrix](src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java)
|
||||
- 📄 [SentinelLinearSearch](src/main/java/com/thealgorithms/searches/SentinelLinearSearch.java)
|
||||
- 📄 [SortOrderAgnosticBinarySearch](src/main/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearch.java)
|
||||
- 📄 [SquareRootBinarySearch](src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java)
|
||||
- 📄 [TernarySearch](src/main/java/com/thealgorithms/searches/TernarySearch.java)
|
||||
- 📄 [UnionFind](src/main/java/com/thealgorithms/searches/UnionFind.java)
|
||||
@@ -817,7 +813,6 @@
|
||||
- 📄 [LetterCombinationsOfPhoneNumber](src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java)
|
||||
- 📄 [LongestCommonPrefix](src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java)
|
||||
- 📄 [LongestNonRepetitiveSubstring](src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java)
|
||||
- 📄 [LongestPalindromicSubstring](src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java)
|
||||
- 📄 [Lower](src/main/java/com/thealgorithms/strings/Lower.java)
|
||||
- 📄 [Manacher](src/main/java/com/thealgorithms/strings/Manacher.java)
|
||||
- 📄 [MyAtoi](src/main/java/com/thealgorithms/strings/MyAtoi.java)
|
||||
@@ -834,7 +829,6 @@
|
||||
- 📄 [StringMatchFiniteAutomata](src/main/java/com/thealgorithms/strings/StringMatchFiniteAutomata.java)
|
||||
- 📄 [SuffixArray](src/main/java/com/thealgorithms/strings/SuffixArray.java)
|
||||
- 📄 [Upper](src/main/java/com/thealgorithms/strings/Upper.java)
|
||||
- 📄 [ValidParentheses](src/main/java/com/thealgorithms/strings/ValidParentheses.java)
|
||||
- 📄 [WordLadder](src/main/java/com/thealgorithms/strings/WordLadder.java)
|
||||
- 📄 [ZAlgorithm](src/main/java/com/thealgorithms/strings/ZAlgorithm.java)
|
||||
- 📁 **zigZagPattern**
|
||||
@@ -1395,7 +1389,6 @@
|
||||
- 📄 [MaximumSumOfDistinctSubarraysWithLengthKTest](src/test/java/com/thealgorithms/others/MaximumSumOfDistinctSubarraysWithLengthKTest.java)
|
||||
- 📄 [MiniMaxAlgorithmTest](src/test/java/com/thealgorithms/others/MiniMaxAlgorithmTest.java)
|
||||
- 📄 [MosAlgorithmTest](src/test/java/com/thealgorithms/others/MosAlgorithmTest.java)
|
||||
- 📄 [NewManShanksPrimeTest](src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java)
|
||||
- 📄 [NextFitTest](src/test/java/com/thealgorithms/others/NextFitTest.java)
|
||||
- 📄 [PageRankTest](src/test/java/com/thealgorithms/others/PageRankTest.java)
|
||||
- 📄 [PasswordGenTest](src/test/java/com/thealgorithms/others/PasswordGenTest.java)
|
||||
@@ -1404,8 +1397,6 @@
|
||||
- 📄 [SkylineProblemTest](src/test/java/com/thealgorithms/others/SkylineProblemTest.java)
|
||||
- 📄 [TwoPointersTest](src/test/java/com/thealgorithms/others/TwoPointersTest.java)
|
||||
- 📄 [WorstFitCPUTest](src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
|
||||
- 📁 **cn**
|
||||
- 📄 [HammingDistanceTest](src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java)
|
||||
- 📁 **physics**
|
||||
- 📄 [CoulombsLawTest](src/test/java/com/thealgorithms/physics/CoulombsLawTest.java)
|
||||
- 📄 [DampedOscillatorTest](src/test/java/com/thealgorithms/physics/DampedOscillatorTest.java)
|
||||
@@ -1479,7 +1470,6 @@
|
||||
- 📄 [LowerBoundTest](src/test/java/com/thealgorithms/searches/LowerBoundTest.java)
|
||||
- 📄 [MonteCarloTreeSearchTest](src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
|
||||
- 📄 [OrderAgnosticBinarySearchTest](src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
|
||||
- 📄 [PerfectBinarySearchTest](src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
|
||||
- 📄 [QuickSelectTest](src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
|
||||
- 📄 [RabinKarpAlgorithmTest](src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
|
||||
- 📄 [RandomSearchTest](src/test/java/com/thealgorithms/searches/RandomSearchTest.java)
|
||||
@@ -1488,7 +1478,6 @@
|
||||
- 📄 [SaddlebackSearchTest](src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
|
||||
- 📄 [SearchInARowAndColWiseSortedMatrixTest](src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
|
||||
- 📄 [SentinelLinearSearchTest](src/test/java/com/thealgorithms/searches/SentinelLinearSearchTest.java)
|
||||
- 📄 [SortOrderAgnosticBinarySearchTest](src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
|
||||
- 📄 [SquareRootBinarySearchTest](src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
|
||||
- 📄 [TernarySearchTest](src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
|
||||
- 📄 [TestSearchInARowAndColWiseSortedMatrix](src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
|
||||
@@ -1593,7 +1582,6 @@
|
||||
- 📄 [LetterCombinationsOfPhoneNumberTest](src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java)
|
||||
- 📄 [LongestCommonPrefixTest](src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java)
|
||||
- 📄 [LongestNonRepetitiveSubstringTest](src/test/java/com/thealgorithms/strings/LongestNonRepetitiveSubstringTest.java)
|
||||
- 📄 [LongestPalindromicSubstringTest](src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java)
|
||||
- 📄 [LowerTest](src/test/java/com/thealgorithms/strings/LowerTest.java)
|
||||
- 📄 [ManacherTest](src/test/java/com/thealgorithms/strings/ManacherTest.java)
|
||||
- 📄 [MyAtoiTest](src/test/java/com/thealgorithms/strings/MyAtoiTest.java)
|
||||
@@ -1609,7 +1597,6 @@
|
||||
- 📄 [StringMatchFiniteAutomataTest](src/test/java/com/thealgorithms/strings/StringMatchFiniteAutomataTest.java)
|
||||
- 📄 [SuffixArrayTest](src/test/java/com/thealgorithms/strings/SuffixArrayTest.java)
|
||||
- 📄 [UpperTest](src/test/java/com/thealgorithms/strings/UpperTest.java)
|
||||
- 📄 [ValidParenthesesTest](src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java)
|
||||
- 📄 [WordLadderTest](src/test/java/com/thealgorithms/strings/WordLadderTest.java)
|
||||
- 📄 [ZAlgorithmTest](src/test/java/com/thealgorithms/strings/ZAlgorithmTest.java)
|
||||
- 📁 **zigZagPattern**
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.thealgorithms.others.cn;
|
||||
|
||||
public final class HammingDistance {
|
||||
private HammingDistance() {
|
||||
}
|
||||
|
||||
private static void checkChar(char inChar) {
|
||||
if (inChar != '0' && inChar != '1') {
|
||||
throw new IllegalArgumentException("Input must be a binary string.");
|
||||
}
|
||||
}
|
||||
|
||||
public static int compute(char charA, char charB) {
|
||||
checkChar(charA);
|
||||
checkChar(charB);
|
||||
return charA == charB ? 0 : 1;
|
||||
}
|
||||
|
||||
public static int compute(String bitsStrA, String bitsStrB) {
|
||||
if (bitsStrA.length() != bitsStrB.length()) {
|
||||
throw new IllegalArgumentException("Input strings must have the same length.");
|
||||
}
|
||||
|
||||
int totalErrorBitCount = 0;
|
||||
|
||||
for (int i = 0; i < bitsStrA.length(); i++) {
|
||||
totalErrorBitCount += compute(bitsStrA.charAt(i), bitsStrB.charAt(i));
|
||||
}
|
||||
|
||||
return totalErrorBitCount;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
|
||||
/**
|
||||
* Binary search is one of the most popular algorithms The algorithm finds the
|
||||
* position of a target value within a sorted array
|
||||
*
|
||||
* <p>
|
||||
* Worst-case performance O(log n) Best-case performance O(1) Average
|
||||
* performance O(log n) Worst-case space complexity O(1)
|
||||
*
|
||||
* @author D Sunil (https://github.com/sunilnitdgp)
|
||||
* @see SearchAlgorithm
|
||||
*/
|
||||
|
||||
public class PerfectBinarySearch<T> implements SearchAlgorithm {
|
||||
|
||||
/**
|
||||
* @param array is an array where the element should be found
|
||||
* @param key is an element which should be found
|
||||
* @param <T> is any comparable type
|
||||
* @return index of the element
|
||||
*/
|
||||
@Override
|
||||
public <T extends Comparable<T>> int find(T[] array, T key) {
|
||||
return search(array, key, 0, array.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method implements the Generic Binary Search iteratively.
|
||||
*
|
||||
* @param array The array to make the binary search
|
||||
* @param key The number you are looking for
|
||||
* @return the location of the key, or -1 if not found
|
||||
*/
|
||||
private static <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
|
||||
while (left <= right) {
|
||||
int median = (left + right) >>> 1;
|
||||
int comp = key.compareTo(array[median]);
|
||||
|
||||
if (comp == 0) {
|
||||
return median; // Key found
|
||||
}
|
||||
|
||||
if (comp < 0) {
|
||||
right = median - 1; // Adjust the right bound
|
||||
} else {
|
||||
left = median + 1; // Adjust the left bound
|
||||
}
|
||||
}
|
||||
return -1; // Key not found
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.thealgorithms.searches;
|
||||
public final class SortOrderAgnosticBinarySearch {
|
||||
private SortOrderAgnosticBinarySearch() {
|
||||
}
|
||||
public static int find(int[] arr, int key) {
|
||||
int start = 0;
|
||||
int end = arr.length - 1;
|
||||
boolean arrDescending = arr[start] > arr[end]; // checking for Array is in ascending order or descending order.
|
||||
while (start <= end) {
|
||||
int mid = end - start / 2;
|
||||
if (arr[mid] == key) {
|
||||
return mid;
|
||||
}
|
||||
if (arrDescending) { // boolean is true then our array is in descending order
|
||||
if (key < arr[mid]) {
|
||||
start = mid + 1;
|
||||
} else {
|
||||
end = mid - 1;
|
||||
}
|
||||
} else { // otherwise our array is in ascending order
|
||||
if (key > arr[mid]) {
|
||||
start = mid + 1;
|
||||
} else {
|
||||
end = mid - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
final class LongestPalindromicSubstring {
|
||||
private LongestPalindromicSubstring() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the longest palindromic substring in the given string.
|
||||
*
|
||||
* @param s the input string
|
||||
* @return the longest palindromic substring
|
||||
*/
|
||||
public static String longestPalindrome(String s) {
|
||||
if (s == null || s.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
String maxStr = "";
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
for (int j = i; j < s.length(); ++j) {
|
||||
if (isValid(s, i, j) && (j - i + 1 > maxStr.length())) {
|
||||
maxStr = s.substring(i, j + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxStr;
|
||||
}
|
||||
|
||||
private static boolean isValid(String s, int lo, int hi) {
|
||||
int n = hi - lo + 1;
|
||||
for (int i = 0; i < n / 2; ++i) {
|
||||
if (s.charAt(lo + i) != s.charAt(hi - i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Validates if a given string has valid matching parentheses.
|
||||
* <p>
|
||||
* A string is considered valid if:
|
||||
* <ul>
|
||||
* <li>Open brackets are closed by the same type of brackets.</li>
|
||||
* <li>Brackets are closed in the correct order.</li>
|
||||
* <li>Every closing bracket has a corresponding open bracket of the same type.</li>
|
||||
* </ul>
|
||||
*
|
||||
* Allowed characters: '(', ')', '{', '}', '[', ']'
|
||||
*/
|
||||
public final class ValidParentheses {
|
||||
private ValidParentheses() {
|
||||
}
|
||||
|
||||
private static final Map<Character, Character> BRACKET_PAIRS = Map.of(')', '(', '}', '{', ']', '[');
|
||||
|
||||
/**
|
||||
* Checks if the input string has valid parentheses.
|
||||
*
|
||||
* @param s the string containing only bracket characters
|
||||
* @return true if valid, false otherwise
|
||||
* @throws IllegalArgumentException if the string contains invalid characters or is null
|
||||
*/
|
||||
public static boolean isValid(String s) {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Input string cannot be null");
|
||||
}
|
||||
|
||||
Deque<Character> stack = new ArrayDeque<>();
|
||||
|
||||
for (char c : s.toCharArray()) {
|
||||
if (BRACKET_PAIRS.containsValue(c)) {
|
||||
stack.push(c); // opening bracket
|
||||
} else if (BRACKET_PAIRS.containsKey(c)) {
|
||||
if (stack.isEmpty() || stack.pop() != BRACKET_PAIRS.get(c)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unexpected character: " + c);
|
||||
}
|
||||
}
|
||||
|
||||
return stack.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -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