Skip to content

Commit ffc9652

Browse files
authored
Merge branch 'master' into refactor/FordFulkerson
2 parents e5d0aac + e5c0e4b commit ffc9652

File tree

4 files changed

+79
-67
lines changed

4 files changed

+79
-67
lines changed

src/main/java/com/thealgorithms/others/TwoPointers.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Arrays;
4-
53
/**
6-
* The two pointer technique is a useful tool to utilize when searching for
4+
* The two-pointer technique is a useful tool to utilize when searching for
75
* pairs in a sorted array.
86
*
97
* <p>
10-
* link: https://www.geeksforgeeks.org/two-pointers-technique/
8+
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
119
*/
1210
final class TwoPointers {
1311
private TwoPointers() {
1412
}
1513

1614
/**
17-
* Given a sorted array arr (sorted in ascending order). Find if there
18-
* exists any pair of elements such that their sum is equal to key.
15+
* Given a sorted array arr (sorted in ascending order), find if there exists
16+
* any pair of elements such that their sum is equal to the key.
1917
*
20-
* @param arr the array contains elements
18+
* @param arr the array containing elements (must be sorted in ascending order)
2119
* @param key the number to search
22-
* @return {@code true} if there exists a pair of elements, {@code false}
23-
* otherwise.
20+
* @return {@code true} if there exists a pair of elements, {@code false} otherwise.
2421
*/
2522
public static boolean isPairedSum(int[] arr, int key) {
26-
/* array sorting is necessary for this algorithm to function correctly */
27-
Arrays.sort(arr);
28-
int i = 0;
29-
/* index of first element */
30-
int j = arr.length - 1;
31-
/* index of last element */
23+
int i = 0; // index of the first element
24+
int j = arr.length - 1; // index of the last element
3225

3326
while (i < j) {
34-
if (arr[i] + arr[j] == key) {
27+
int sum = arr[i] + arr[j];
28+
if (sum == key) {
3529
return true;
36-
} else if (arr[i] + arr[j] < key) {
30+
} else if (sum < key) {
3731
i++;
3832
} else {
3933
j--;
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
54

65
import java.util.List;
7-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
810

911
class PrimeFactorizationTest {
1012

11-
@Test
12-
void testpFactorsMustReturnEmptyList() {
13-
// given
14-
int n = 0;
15-
16-
// then
17-
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
13+
@ParameterizedTest
14+
@MethodSource("provideNumbersAndFactors")
15+
void testPrimeFactorization(int number, List<Integer> expectedFactors) {
16+
assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
1817
}
1918

20-
@Test
21-
void testpFactorsMustReturnNonEmptyList() {
22-
// given
23-
int n = 198;
24-
int expectedListSize = 4;
19+
@ParameterizedTest
20+
@MethodSource("provideNumbersAndSizes")
21+
void testPrimeFactorsSize(int number, int expectedSize) {
22+
assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
23+
}
2524

26-
// when
27-
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
25+
private static Stream<Arguments> provideNumbersAndFactors() {
26+
return Stream.of(Arguments.of(0, List.of()), Arguments.of(1, List.of()), Arguments.of(2, List.of(2)), Arguments.of(3, List.of(3)), Arguments.of(4, List.of(2, 2)), Arguments.of(18, List.of(2, 3, 3)), Arguments.of(100, List.of(2, 2, 5, 5)), Arguments.of(198, List.of(2, 3, 3, 11)));
27+
}
2828

29-
// then
30-
assertEquals(expectedListSize, actualResultList.size());
31-
assertEquals(2, actualResultList.get(0));
32-
assertEquals(3, actualResultList.get(1));
33-
assertEquals(3, actualResultList.get(2));
34-
assertEquals(11, actualResultList.get(3));
29+
private static Stream<Arguments> provideNumbersAndSizes() {
30+
return Stream.of(Arguments.of(2, 1), Arguments.of(3, 1), Arguments.of(4, 2), Arguments.of(18, 3), Arguments.of(100, 4), Arguments.of(198, 4));
3531
}
3632
}

src/test/java/com/thealgorithms/maths/ReverseNumberTest.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
7-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
89

910
public class ReverseNumberTest {
1011

11-
@Test
12-
public void testReverseNumber() {
13-
HashMap<Integer, Integer> testCases = new HashMap<>();
14-
testCases.put(0, 0);
15-
testCases.put(1, 1);
16-
testCases.put(10, 1);
17-
testCases.put(123, 321);
18-
testCases.put(7890, 987);
19-
20-
for (final var tc : testCases.entrySet()) {
21-
assertEquals(ReverseNumber.reverseNumber(tc.getKey()), tc.getValue());
22-
}
12+
@ParameterizedTest
13+
@CsvSource({"0, 0", "1, 1", "10, 1", "123, 321", "7890, 987"})
14+
public void testReverseNumber(int input, int expected) {
15+
assertEquals(expected, ReverseNumber.reverseNumber(input));
2316
}
2417

25-
@Test
26-
public void testReverseNumberThrowsExceptionForNegativeInput() {
27-
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(-1));
18+
@ParameterizedTest
19+
@ValueSource(ints = {-1, -123, -7890})
20+
public void testReverseNumberThrowsExceptionForNegativeInput(int input) {
21+
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(input));
2822
}
2923
}

src/test/java/com/thealgorithms/others/TwoPointersTest.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,65 @@
88
public class TwoPointersTest {
99

1010
@Test
11-
void twoPointersFirstTestCase() {
11+
void testPositivePairExists() {
1212
int[] arr = {2, 6, 9, 22, 121};
1313
int key = 28;
1414
assertTrue(TwoPointers.isPairedSum(arr, key));
1515
}
1616

1717
@Test
18-
void twoPointersSecondTestCase() {
19-
int[] arr = {-1, -12, 12, 0, 8};
18+
void testNegativePairExists() {
19+
int[] arr = {-12, -1, 0, 8, 12};
2020
int key = 0;
2121
assertTrue(TwoPointers.isPairedSum(arr, key));
2222
}
2323

2424
@Test
25-
void twoPointersThirdTestCase() {
26-
int[] arr = {12, 35, 12, 152, 0};
25+
void testPairDoesNotExist() {
26+
int[] arr = {0, 12, 12, 35, 152};
2727
int key = 13;
2828
assertFalse(TwoPointers.isPairedSum(arr, key));
2929
}
3030

3131
@Test
32-
void twoPointersFourthTestCase() {
33-
int[] arr = {-2, 5, -1, 52, 31};
34-
int key = -3;
32+
void testNegativeSumPair() {
33+
int[] arr = {-10, -3, 1, 2, 5, 9};
34+
int key = -8;
3535
assertTrue(TwoPointers.isPairedSum(arr, key));
3636
}
3737

3838
@Test
39-
void twoPointersFiftiethTestCase() {
40-
int[] arr = {25, 1, 0, 61, 21};
41-
int key = 12;
39+
void testPairDoesNotExistWithPositiveSum() {
40+
int[] arr = {1, 2, 3, 4, 5};
41+
int key = 10;
4242
assertFalse(TwoPointers.isPairedSum(arr, key));
4343
}
44+
45+
@Test
46+
void testEmptyArray() {
47+
int[] arr = {};
48+
int key = 5;
49+
assertFalse(TwoPointers.isPairedSum(arr, key));
50+
}
51+
52+
@Test
53+
void testSingleElementArray() {
54+
int[] arr = {5};
55+
int key = 5;
56+
assertFalse(TwoPointers.isPairedSum(arr, key));
57+
}
58+
59+
@Test
60+
void testArrayWithDuplicateElements() {
61+
int[] arr = {1, 1, 3, 5, 5};
62+
int key = 6;
63+
assertTrue(TwoPointers.isPairedSum(arr, key));
64+
}
65+
66+
@Test
67+
void testPairExistsAtEdges() {
68+
int[] arr = {1, 3, 4, 7, 8};
69+
int key = 9;
70+
assertTrue(TwoPointers.isPairedSum(arr, key));
71+
}
4472
}

0 commit comments

Comments
 (0)