diff --git a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java index 488476c..06de614 100644 --- a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java +++ b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java @@ -442,7 +442,40 @@ public class FullyUsedArrayProcessor implements SequenceProcessor, FullyUsedArray> divideByPivot(FullyUsedArray array, T pivot, Comparator cmp) { - throw new UnsupportedOperationException(); + var p = new Pair, FullyUsedArray>(); + p.fst = new FullyUsedArray<>(); + p.snd = new FullyUsedArray<>(); + + var less = 0; + var greater = 0; + + for (int i = 0; i < array.theArray.length; i++) { + var c = cmp.compare(array.theArray[i], pivot); + if (c < 0) { + less++; + } + if (c > 0) { + greater++; + } + } + + p.fst.theArray = Arrays.newArray(less); + p.snd.theArray = Arrays.newArray(greater); + + var indexA = 0; + var indexB = 0; + + for (int i = 0; i < array.theArray.length; i++) { + var c = cmp.compare(array.theArray[i], pivot); + if (c < 0) { + p.fst.theArray[indexA++] = array.theArray[i]; + } + if (c > 0) { + p.snd.theArray[indexB++] = array.theArray[i]; + } + } + + return p; } @Override diff --git a/test/aud/exam/prep/SequenceProcessorTest.java b/test/aud/exam/prep/SequenceProcessorTest.java index 66756d0..10a6248 100644 --- a/test/aud/exam/prep/SequenceProcessorTest.java +++ b/test/aud/exam/prep/SequenceProcessorTest.java @@ -503,7 +503,7 @@ public abstract class SequenceProcessorTest { } @ParameterizedTest - @ArgumentsSource(DoubleListProvider.class) + @ArgumentsSource(ListProvider.class) void testThat_divideAlternatingWorks(List list) { S s = processor.create(list); var div = List.of(new ArrayList(), new ArrayList()); @@ -520,4 +520,27 @@ public abstract class SequenceProcessorTest { assertTrue(processor.check(p.snd)); assertIterableEquals(div.get(1), processor.iterate(p.snd)); } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_divideByPivotWorks(List list) { + S s = processor.create(list); + + var pivot = list.isEmpty() ? 50 : list.get(0); + var div = list + .stream() + .collect(Collectors.groupingBy(n -> + (int) Math.signum(cmp.compare(n, pivot)))); + + div.putIfAbsent(-1, List.of()); + div.putIfAbsent(+1, List.of()); + + var p = processor.divideByPivot(s, pivot, cmp); + + assertTrue(processor.check(p.fst)); + assertIterableEquals(div.get(-1), processor.iterate(p.fst)); + + assertTrue(processor.check(p.snd)); + assertIterableEquals(div.get(+1), processor.iterate(p.snd)); + } } \ No newline at end of file