Compare commits
2 Commits
03d916fa31
...
6f2b3842cd
Author | SHA1 | Date |
---|---|---|
Moritz Böhme | 6f2b3842cd | |
Moritz Böhme | fa48438183 |
|
@ -1,11 +1,47 @@
|
|||
package WardrobeBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class WardrobeBuilder {
|
||||
public static List<List<Integer>> allCombinations(List<Integer> elememts, int target) {
|
||||
List<Integer> wardrobe = List.of(50);
|
||||
return List.of(wardrobe);
|
||||
Set<Integer> elements;
|
||||
int target;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import WardrobeBuilder.WardrobeBuilder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
@ -8,16 +9,53 @@ class MainTest {
|
|||
|
||||
@org.junit.jupiter.api.Test
|
||||
void onlyOneWardrobeFromOneElement() {
|
||||
List<Integer> elememts = List.of(50);
|
||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
||||
Set<Integer> elememts = Set.of(50);
|
||||
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, 50);
|
||||
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||
assertEquals(1, wardrobes.size());
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void sameHeightFromOneElement() {
|
||||
List<Integer> elememts = List.of(50);
|
||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
||||
List<Integer> wardrobe = wardrobes.getFirst();
|
||||
assertEquals(wardrobe, elememts);
|
||||
void targetHeightFromOneElement() {
|
||||
Integer target = 75;
|
||||
Set<Integer> elememts = Set.of(target);
|
||||
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());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
|
16
wardrobe.iml
16
wardrobe.iml
|
@ -24,5 +24,21 @@
|
|||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library" scope="TEST">
|
||||
<library name="JUnit5.8.1">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
Loading…
Reference in New Issue