diff --git a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java index d6d7ddb..488476c 100644 --- a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java +++ b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java @@ -417,7 +417,22 @@ public class FullyUsedArrayProcessor implements SequenceProcessor, FullyUsedArray> divideAlternating(FullyUsedArray array) { - throw new UnsupportedOperationException(); + var p = new Pair, FullyUsedArray>(); + p.fst = new FullyUsedArray<>(); + p.snd = new FullyUsedArray<>(); + + p.snd.theArray = Arrays.newArray(array.theArray.length / 2); + p.fst.theArray = Arrays.newArray(array.theArray.length - p.snd.theArray.length); + + for (int i = 0; i < array.theArray.length; i++) { + if (i % 2 == 0) { + p.fst.theArray[i/2] = array.theArray[i]; + } else { + p.snd.theArray[i/2] = array.theArray[i]; + } + } + + return p; } @Override diff --git a/test/aud/exam/prep/SequenceProcessorTest.java b/test/aud/exam/prep/SequenceProcessorTest.java index de0273a..66756d0 100644 --- a/test/aud/exam/prep/SequenceProcessorTest.java +++ b/test/aud/exam/prep/SequenceProcessorTest.java @@ -501,4 +501,23 @@ public abstract class SequenceProcessorTest { assertTrue(processor.check(s)); assertIterableEquals(listA, processor.iterate(s)); } + + @ParameterizedTest + @ArgumentsSource(DoubleListProvider.class) + void testThat_divideAlternatingWorks(List list) { + S s = processor.create(list); + var div = List.of(new ArrayList(), new ArrayList()); + + for (int i = 0; i < list.size(); i++) { + div.get(i % 2).add(list.get(i)); + } + + var p = processor.divideAlternating(s); + + assertTrue(processor.check(p.fst)); + assertIterableEquals(div.get(0), processor.iterate(p.fst)); + + assertTrue(processor.check(p.snd)); + assertIterableEquals(div.get(1), processor.iterate(p.snd)); + } } \ No newline at end of file