test: add some more
parent
6f2b3842cd
commit
bbc5af9777
|
@ -1,5 +1,3 @@
|
|||
package WardrobeBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -15,9 +13,9 @@ public class WardrobeBuilder {
|
|||
}
|
||||
|
||||
public Set<List<Integer>> allCombinations() {
|
||||
Set<List<Integer>> combinations = completeCombination(new ArrayList<>());
|
||||
Set<List<Integer>> combinations = Set.of(new ArrayList<>());
|
||||
Set<List<Integer>> newCombinations;
|
||||
for (Integer element : elements) {
|
||||
while (anyIncompleteCombinations(combinations)) {
|
||||
newCombinations = new HashSet<>();
|
||||
for (List<Integer> combination : combinations) {
|
||||
newCombinations.addAll(completeCombination(combination));
|
||||
|
@ -27,15 +25,19 @@ public class WardrobeBuilder {
|
|||
return combinations;
|
||||
}
|
||||
|
||||
private boolean anyIncompleteCombinations(Set<List<Integer>> combinations) {
|
||||
return !combinations.stream().allMatch(this::isElementSumEqualToTarget);
|
||||
}
|
||||
|
||||
private Set<List<Integer>> completeCombination(List<Integer> combination) {
|
||||
Integer currentSum = combination.stream().reduce(0, Integer::sum);
|
||||
if (currentSum == target) {
|
||||
Integer sum = calculateSum(combination);
|
||||
if (isElementSumEqualToTarget(sum)) {
|
||||
return Set.of(combination);
|
||||
}
|
||||
|
||||
Set<List<Integer>> combinations = new HashSet<>();
|
||||
for (Integer element : elements) {
|
||||
if (currentSum + element <= target) {
|
||||
if (sum + element <= target) {
|
||||
List<Integer> newCombination = new ArrayList<>(combination);
|
||||
newCombination.add(element);
|
||||
newCombination.sort(Integer::compareTo);
|
||||
|
@ -44,4 +46,16 @@ public class WardrobeBuilder {
|
|||
}
|
||||
return combinations;
|
||||
}
|
||||
|
||||
private boolean isElementSumEqualToTarget(List<Integer> combination) {
|
||||
Integer sum = calculateSum(combination);
|
||||
return sum == target;
|
||||
}
|
||||
private boolean isElementSumEqualToTarget(Integer sum) {
|
||||
return sum == target;
|
||||
}
|
||||
|
||||
private static Integer calculateSum(List<Integer> combination) {
|
||||
return combination.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
import WardrobeBuilder.WardrobeBuilder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
Loading…
Reference in New Issue