Skip to content

Commit 82aab4e

Browse files
committed
refactor: cleanup
1 parent d999aae commit 82aab4e

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44
import java.util.List;
55

66
/**
7-
* Finds all permutations of 1...n of length k
8-
* @author TheClerici (<a href="https://github.com/TheClerici">git-TheClerici</a>)
7+
* Finds all combinations of 0...n-1 of length k
98
*/
109
public final class ArrayCombination {
1110
private ArrayCombination() {
1211
}
1312

1413
/**
15-
* Find all combinations of 1..n using backtracking.
14+
* Finds all combinations of length k of 0..n-1 using backtracking.
1615
*
17-
* @param n Max value of the elements.
16+
* @param n Number of the elements.
1817
* @param k Length of the combination.
1918
* @return A list of all combinations of length k.
20-
* Returns an empty list if k is 0 or n is less than k.
2119
*/
2220
public static List<List<Integer>> combination(int n, int k) {
23-
if (k <= 0 || n < k) {
24-
return new ArrayList<>(); // Return empty list for invalid input
21+
if (n < 0 || k < 0 || k > n) {
22+
throw new IllegalArgumentException("Wrong input.");
2523
}
2624

2725
List<List<Integer>> combinations = new ArrayList<>();
28-
combine(combinations, new ArrayList<>(), 1, n, k);
26+
combine(combinations, new ArrayList<>(), 0, n, k);
2927
return combinations;
3028
}
3129

@@ -35,10 +33,10 @@ private static void combine(List<List<Integer>> combinations, List<Integer> curr
3533
return;
3634
}
3735

38-
for (int i = start; i <= n; i++) {
36+
for (int i = start; i < n; i++) {
3937
current.add(i);
40-
combine(combinations, current, i + 1, n, k); // Recursive call
41-
current.remove(current.size() - 1); // Backtrack
38+
combine(combinations, current, i + 1, n, k);
39+
current.removeLast(); // Backtrack
4240
}
4341
}
4442
}
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,36 @@
11
package com.thealgorithms.backtracking;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

6+
import com.thealgorithms.maths.BinomialCoefficient;
57
import java.util.ArrayList;
68
import java.util.List;
7-
import org.junit.jupiter.api.Test;
9+
import java.util.stream.Stream;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
813

914
public class ArrayCombinationTest {
10-
11-
@Test
12-
public void testCombination() {
13-
// Test case 1: n = 4, k = 2
14-
List<List<Integer>> expected = new ArrayList<>();
15-
expected.add(List.of(1, 2));
16-
expected.add(List.of(1, 3));
17-
expected.add(List.of(1, 4));
18-
expected.add(List.of(2, 3));
19-
expected.add(List.of(2, 4));
20-
expected.add(List.of(3, 4));
21-
22-
List<List<Integer>> actual = ArrayCombination.combination(4, 2);
23-
24-
assertEquals(expected, actual);
15+
@ParameterizedTest
16+
@MethodSource("regularInputs")
17+
void testCombination(int n, int k, List<List<Integer>> expected) {
18+
assertEquals(expected.size(), BinomialCoefficient.binomialCoefficient(n, k));
19+
assertEquals(expected, ArrayCombination.combination(n, k));
2520
}
2621

27-
@Test
28-
public void testEmptyCombination() {
29-
// Test case 2: n = 4, k = 0 (invalid input)
30-
List<List<Integer>> expected = new ArrayList<>();
31-
List<List<Integer>> actual = ArrayCombination.combination(4, 0);
32-
33-
assertEquals(expected, actual);
22+
@ParameterizedTest
23+
@MethodSource("wrongInputs")
24+
void testCombinationThrows(int n, int k) {
25+
assertThrows(IllegalArgumentException.class, () -> ArrayCombination.combination(n, k));
3426
}
3527

36-
@Test
37-
public void testSingleElementCombinations() {
38-
// Test case 3: n = 3, k = 1
39-
List<List<Integer>> expected = new ArrayList<>();
40-
expected.add(List.of(1));
41-
expected.add(List.of(2));
42-
expected.add(List.of(3));
43-
44-
List<List<Integer>> actual = ArrayCombination.combination(3, 1);
28+
private static Stream<Arguments> regularInputs() {
29+
return Stream.of(Arguments.of(0, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 1, List.of(List.of(0))), Arguments.of(3, 0, List.of(new ArrayList<Integer>())), Arguments.of(3, 1, List.of(List.of(0), List.of(1), List.of(2))),
30+
Arguments.of(4, 2, List.of(List.of(0, 1), List.of(0, 2), List.of(0, 3), List.of(1, 2), List.of(1, 3), List.of(2, 3))));
31+
}
4532

46-
assertEquals(expected, actual);
33+
private static Stream<Arguments> wrongInputs() {
34+
return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100));
4735
}
4836
}

0 commit comments

Comments
 (0)