mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Migrate recursive reverse string method and tests to ReverseString (#6504)
* Migrate recursive reverse string method and tests to ReverseString - Moved recursive reverse method from ReverseStringRecursive.java to ReverseString.java - Moved tests from ReverseStringRecursiveTest.java to ReverseStringTest.java - Deleted old ReverseStringRecursive files * Fix formatting for ReverseStringTest.java as per clang-format linter
This commit is contained in:
@@ -86,4 +86,17 @@ public final class ReverseString {
|
||||
}
|
||||
return reversedString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the String using Recursion
|
||||
* @param str string to be reversed
|
||||
* @return reversed string
|
||||
*/
|
||||
public static String reverseStringUsingRecursion(String str) {
|
||||
if (str.isEmpty()) {
|
||||
return str;
|
||||
} else {
|
||||
return reverseStringUsingRecursion(str.substring(1)) + str.charAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
/**
|
||||
* Reverse String using Recursion
|
||||
*/
|
||||
public final class ReverseStringRecursive {
|
||||
private ReverseStringRecursive() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param str string to be reversed
|
||||
* @return reversed string
|
||||
*/
|
||||
public static String reverse(String str) {
|
||||
if (str.isEmpty()) {
|
||||
return str;
|
||||
} else {
|
||||
return reverse(str.substring(1)) + str.charAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
public class ReverseStringRecursiveTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'!123 ABC xyz!', '!zyx CBA 321!'", "'Abc 123 Xyz', 'zyX 321 cbA'", "'12.34,56;78:90', '09:87;65,43.21'", "'abcdEFGHiJKL', 'LKJiHGFEdcba'",
|
||||
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
|
||||
public void
|
||||
testReverseString(String input, String expectedOutput) {
|
||||
assertEquals(expectedOutput, ReverseStringRecursive.reverse(input));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class ReverseStringTest {
|
||||
@@ -44,4 +45,12 @@ public class ReverseStringTest {
|
||||
public void testReverseStringUsingStackWithNullInput() {
|
||||
assertThrows(IllegalArgumentException.class, () -> ReverseString.reverseStringUsingStack(null));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'!123 ABC xyz!', '!zyx CBA 321!'", "'Abc 123 Xyz', 'zyX 321 cbA'", "'12.34,56;78:90', '09:87;65,43.21'", "'abcdEFGHiJKL', 'LKJiHGFEdcba'",
|
||||
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
|
||||
public void
|
||||
testReverseStringUsingRecursion(String input, String expectedOutput) {
|
||||
assertEquals(expectedOutput, ReverseString.reverseStringUsingRecursion(input));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user