From 6f2b3842cdd839a25f731db36c9f177a318618cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Tue, 2 Jul 2024 11:08:28 +0200 Subject: [PATCH] test: add some more --- .../java/WardrobeBuilder/WardrobeBuilder.java | 41 ++++++++++++++-- src/test/java/MainTest.java | 49 ++++++++++++++++--- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/main/java/WardrobeBuilder/WardrobeBuilder.java b/src/main/java/WardrobeBuilder/WardrobeBuilder.java index f9cae57..26e84d9 100644 --- a/src/main/java/WardrobeBuilder/WardrobeBuilder.java +++ b/src/main/java/WardrobeBuilder/WardrobeBuilder.java @@ -1,12 +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> allCombinations(Set elememts, int target) { - List wardrobe = List.of(50); - return List.of(wardrobe); + Set elements; + int target; + + public WardrobeBuilder(Set elements, int target){ + this.elements = elements; + this.target = target; + } + + public Set> allCombinations() { + Set> combinations = completeCombination(new ArrayList<>()); + Set> newCombinations; + for (Integer element : elements) { + newCombinations = new HashSet<>(); + for (List combination : combinations) { + newCombinations.addAll(completeCombination(combination)); + } + combinations = newCombinations; + } + return combinations; + } + + private Set> completeCombination(List combination) { + Integer currentSum = combination.stream().reduce(0, Integer::sum); + if (currentSum == target) { + return Set.of(combination); + } + + Set> combinations = new HashSet<>(); + for (Integer element : elements) { + if (currentSum + element <= target) { + List newCombination = new ArrayList<>(combination); + newCombination.add(element); + newCombination.sort(Integer::compareTo); + combinations.add(newCombination); + } + } + return combinations; } } diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java index b4655f3..bfc4ef3 100644 --- a/src/test/java/MainTest.java +++ b/src/test/java/MainTest.java @@ -10,17 +10,52 @@ class MainTest { @org.junit.jupiter.api.Test void onlyOneWardrobeFromOneElement() { Set elememts = Set.of(50); - List> wardrobes = WardrobeBuilder.allCombinations(elememts, 50); + WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, 50); + Set> wardrobes = wardrobeBuilder.allCombinations(); assertEquals(1, wardrobes.size()); } @org.junit.jupiter.api.Test - void sameHeightFromOneElement() { - Set elememts = Set.of(50); - List> wardrobes = WardrobeBuilder.allCombinations(elememts, 50); - List wardrobe = wardrobes.getFirst(); + void targetHeightFromOneElement() { + Integer target = 75; + Set elememts = Set.of(target); + WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target); + Set> wardrobes = wardrobeBuilder.allCombinations(); + List wardrobe = wardrobes.iterator().next(); + Integer sum = wardrobe.stream().reduce(0, Integer::sum); + assertEquals(target, sum); + } + + @org.junit.jupiter.api.Test + void targetHeightSumFromTwoElements() { + Set elememts = Set.of(50, 75); + Integer target = 125; + WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target); + + Set> wardrobes = wardrobeBuilder.allCombinations(); + List wardrobe = wardrobes.iterator().next(); + assertEquals(2, wardrobe.size()); + } + + @org.junit.jupiter.api.Test + void noDuplicatesForTwoElements() { + Set elememts = Set.of(50, 75); + Integer target = 125; + WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target); + + Set> wardrobes = wardrobeBuilder.allCombinations(); assertEquals(1, wardrobes.size()); - Integer elememt = wardrobe.getFirst(); - assertEquals(50, elememt); + } + + @org.junit.jupiter.api.Test + void targetHeightPartialFromTwoElements() { + Set elememts = Set.of(50, 75); + Integer target = 50; + WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target); + + Set> wardrobes = wardrobeBuilder.allCombinations(); + List wardrobe = wardrobes.iterator().next(); + Integer sum = wardrobe.stream().reduce(0, Integer::sum); + assertEquals(target, sum); } }