Compare commits
No commits in common. "6f2b3842cdd839a25f731db36c9f177a318618cd" and "03d916fa3167bf95d39841d6e971f41da70ff596" have entirely different histories.
6f2b3842cd
...
03d916fa31
3 changed files with 10 additions and 100 deletions
|
@ -1,47 +1,11 @@
|
|||
package WardrobeBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class WardrobeBuilder {
|
||||
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;
|
||||
public static List<List<Integer>> allCombinations(List<Integer> elememts, int target) {
|
||||
List<Integer> wardrobe = List.of(50);
|
||||
return List.of(wardrobe);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import WardrobeBuilder.WardrobeBuilder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
@ -9,53 +8,16 @@ class MainTest {
|
|||
|
||||
@org.junit.jupiter.api.Test
|
||||
void onlyOneWardrobeFromOneElement() {
|
||||
Set<Integer> elememts = Set.of(50);
|
||||
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, 50);
|
||||
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
|
||||
List<Integer> elememts = List.of(50);
|
||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
||||
assertEquals(1, wardrobes.size());
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
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);
|
||||
void sameHeightFromOneElement() {
|
||||
List<Integer> elememts = List.of(50);
|
||||
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
|
||||
List<Integer> wardrobe = wardrobes.getFirst();
|
||||
assertEquals(wardrobe, elememts);
|
||||
}
|
||||
}
|
||||
|
|
16
wardrobe.iml
16
wardrobe.iml
|
@ -24,21 +24,5 @@
|
|||
<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 a new issue