test: add some more
This commit is contained in:
parent
fa48438183
commit
6f2b3842cd
2 changed files with 80 additions and 10 deletions
|
@ -1,12 +1,47 @@
|
||||||
package WardrobeBuilder;
|
package WardrobeBuilder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class WardrobeBuilder {
|
public class WardrobeBuilder {
|
||||||
public static List<List<Integer>> allCombinations(Set<Integer> elememts, int target) {
|
Set<Integer> elements;
|
||||||
List<Integer> wardrobe = List.of(50);
|
int target;
|
||||||
return List.of(wardrobe);
|
|
||||||
|
public WardrobeBuilder(Set<Integer> elements, int target){
|
||||||
|
this.elements = elements;
|
||||||
|
this.target = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<List<Integer>> allCombinations() {
|
||||||
|
Set<List<Integer>> combinations = completeCombination(new ArrayList<>());
|
||||||
|
Set<List<Integer>> newCombinations;
|
||||||
|
for (Integer element : elements) {
|
||||||
|
newCombinations = new HashSet<>();
|
||||||
|
for (List<Integer> combination : combinations) {
|
||||||
|
newCombinations.addAll(completeCombination(combination));
|
||||||
|
}
|
||||||
|
combinations = newCombinations;
|
||||||
|
}
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<List<Integer>> completeCombination(List<Integer> combination) {
|
||||||
|
Integer currentSum = combination.stream().reduce(0, Integer::sum);
|
||||||
|
if (currentSum == target) {
|
||||||
|
return Set.of(combination);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<List<Integer>> combinations = new HashSet<>();
|
||||||
|
for (Integer element : elements) {
|
||||||
|
if (currentSum + element <= target) {
|
||||||
|
List<Integer> newCombination = new ArrayList<>(combination);
|
||||||
|
newCombination.add(element);
|
||||||
|
newCombination.sort(Integer::compareTo);
|
||||||
|
combinations.add(newCombination);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return combinations;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,52 @@ class MainTest {
|
||||||
@org.junit.jupiter.api.Test
|
@org.junit.jupiter.api.Test
|
||||||
void onlyOneWardrobeFromOneElement() {
|
void onlyOneWardrobeFromOneElement() {
|
||||||
Set<Integer> elememts = Set.of(50);
|
Set<Integer> elememts = Set.of(50);
|
||||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, 50);
|
||||||
|
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||||
assertEquals(1, wardrobes.size());
|
assertEquals(1, wardrobes.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@org.junit.jupiter.api.Test
|
@org.junit.jupiter.api.Test
|
||||||
void sameHeightFromOneElement() {
|
void targetHeightFromOneElement() {
|
||||||
Set<Integer> elememts = Set.of(50);
|
Integer target = 75;
|
||||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
Set<Integer> elememts = Set.of(target);
|
||||||
List<Integer> wardrobe = wardrobes.getFirst();
|
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
|
||||||
|
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||||
|
List<Integer> wardrobe = wardrobes.iterator().next();
|
||||||
|
Integer sum = wardrobe.stream().reduce(0, Integer::sum);
|
||||||
|
assertEquals(target, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void targetHeightSumFromTwoElements() {
|
||||||
|
Set<Integer> elememts = Set.of(50, 75);
|
||||||
|
Integer target = 125;
|
||||||
|
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
|
||||||
|
|
||||||
|
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||||
|
List<Integer> wardrobe = wardrobes.iterator().next();
|
||||||
|
assertEquals(2, wardrobe.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void noDuplicatesForTwoElements() {
|
||||||
|
Set<Integer> elememts = Set.of(50, 75);
|
||||||
|
Integer target = 125;
|
||||||
|
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
|
||||||
|
|
||||||
|
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||||
assertEquals(1, wardrobes.size());
|
assertEquals(1, wardrobes.size());
|
||||||
Integer elememt = wardrobe.getFirst();
|
}
|
||||||
assertEquals(50, elememt);
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void targetHeightPartialFromTwoElements() {
|
||||||
|
Set<Integer> elememts = Set.of(50, 75);
|
||||||
|
Integer target = 50;
|
||||||
|
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
|
||||||
|
|
||||||
|
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||||
|
List<Integer> wardrobe = wardrobes.iterator().next();
|
||||||
|
Integer sum = wardrobe.stream().reduce(0, Integer::sum);
|
||||||
|
assertEquals(target, sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue