Compare commits

...

2 Commits

Author SHA1 Message Date
Moritz Böhme 6f2b3842cd test: add some more 2024-07-02 11:08:28 +02:00
Moritz Böhme fa48438183 refactor: elements type 2024-07-02 10:21:46 +02:00
3 changed files with 100 additions and 10 deletions

View File

@ -1,11 +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;
public class WardrobeBuilder { public class WardrobeBuilder {
public static List<List<Integer>> allCombinations(List<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;
} }
} }

View File

@ -1,6 +1,7 @@
import WardrobeBuilder.WardrobeBuilder; import WardrobeBuilder.WardrobeBuilder;
import java.util.List; import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -8,16 +9,53 @@ class MainTest {
@org.junit.jupiter.api.Test @org.junit.jupiter.api.Test
void onlyOneWardrobeFromOneElement() { void onlyOneWardrobeFromOneElement() {
List<Integer> elememts = List.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() {
List<Integer> elememts = List.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);
assertEquals(wardrobe, elememts); 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);
} }
} }

View File

@ -24,5 +24,21 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </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> </component>
</module> </module>