Skip to content

Commit 347e014

Browse files
authored
Merge branch 'master' into master
2 parents 4d3e08d + aaa2b26 commit 347e014

File tree

6 files changed

+82
-25
lines changed

6 files changed

+82
-25
lines changed
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Arrays;
4-
import java.util.Random;
3+
public final class FindMinRecursion {
54

6-
public class FindMinRecursion {
7-
8-
/**
9-
* Driver Code
10-
*/
11-
public static void main(String[] args) {
12-
Random rand = new Random();
13-
14-
/* rand size */
15-
int size = rand.nextInt(100) + 1;
16-
int[] array = new int[size];
17-
18-
/* init array with rand numbers */
19-
for (int i = 0; i < size; i++) {
20-
array[i] = rand.nextInt() % 100;
21-
}
22-
23-
assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
24-
assert min(array) == Arrays.stream(array).min().getAsInt();
5+
private FindMinRecursion() {
256
}
267

278
/**
@@ -32,7 +13,12 @@ public static void main(String[] args) {
3213
* @param high the index of the last element
3314
* @return min of {@code array}
3415
*/
35-
public static int min(int[] array, int low, int high) {
16+
17+
public static int min(final int[] array, final int low, final int high) {
18+
if (array.length == 0) {
19+
throw new IllegalArgumentException("array must be non-empty.");
20+
}
21+
3622
if (low == high) {
3723
return array[low]; // or array[high]
3824
}
@@ -51,7 +37,7 @@ public static int min(int[] array, int low, int high) {
5137
* @param array contains elements
5238
* @return min value of {@code array}
5339
*/
54-
public static int min(int[] array) {
55-
return array.length == 1 ? array[0] : min(array, 0, array.length);
40+
public static int min(final int[] array) {
41+
return min(array, 0, array.length - 1);
5642
}
5743
}

src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created:- 1/10/2023
33
// File Name should be RecursiveBinarySearch.java
44
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
5-
5+
package com.thealgorithms.searches;
66
import java.util.*;
77

88
// Create a SearchAlgorithm class with a generic type
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.strings;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
6+
public final class ReverseWordsInString {
7+
8+
private ReverseWordsInString() {
9+
}
10+
11+
/**
12+
* @brief Reverses words in the input string
13+
* @param s the input string
14+
* @return A string created by reversing the order of the words in {@code s}
15+
*/
16+
17+
public static String reverseWordsInString(final String s) {
18+
var words = s.trim().split("\\s+");
19+
Collections.reverse(Arrays.asList(words));
20+
return String.join(" ", words);
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class FindMinRecursionTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("inputStream")
16+
void numberTests(int expected, int[] input) {
17+
Assertions.assertEquals(expected, FindMinRecursion.min(input));
18+
}
19+
20+
private static Stream<Arguments> inputStream() {
21+
return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(-1, new int[] {-1, 0}), Arguments.of(-10, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(-4, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
22+
}
23+
24+
@Test
25+
public void testFindMaxThrowsExceptionForEmptyInput() {
26+
assertThrows(IllegalArgumentException.class, () -> FindMinRecursion.min(new int[] {}));
27+
}
28+
}

src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Created by Pronay Debnath
22
// Date:- 1/10/2023
33
// Test file updated with JUnit tests
4+
package com.thealgorithms.searches;
45

56
import static org.junit.jupiter.api.Assertions.*;
67

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithms.strings;
2+
3+
import java.util.stream.Stream;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class ReverseWordsInStringTest {
11+
@ParameterizedTest
12+
@MethodSource("inputStream")
13+
void numberTests(String expected, String input) {
14+
Assertions.assertEquals(expected, ReverseWordsInString.reverseWordsInString(input));
15+
}
16+
17+
private static Stream<Arguments> inputStream() {
18+
return Stream.of(Arguments.of("blue is Sky", "Sky is blue"), Arguments.of("blue is Sky", "Sky \n is \t \n blue "), Arguments.of("", ""), Arguments.of("", " "), Arguments.of("", "\t"));
19+
}
20+
}

0 commit comments

Comments
 (0)