Implement divideAlternating
This commit is contained in:
@ -417,7 +417,22 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternating(FullyUsedArray<T> array) {
|
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternating(FullyUsedArray<T> array) {
|
||||||
throw new UnsupportedOperationException();
|
var p = new Pair<FullyUsedArray<T>, FullyUsedArray<T>>();
|
||||||
|
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
|
@Override
|
||||||
|
@ -501,4 +501,23 @@ public abstract class SequenceProcessorTest<S> {
|
|||||||
assertTrue(processor.check(s));
|
assertTrue(processor.check(s));
|
||||||
assertIterableEquals(listA, processor.iterate(s));
|
assertIterableEquals(listA, processor.iterate(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ArgumentsSource(DoubleListProvider.class)
|
||||||
|
void testThat_divideAlternatingWorks(List<Integer> list) {
|
||||||
|
S s = processor.create(list);
|
||||||
|
var div = List.of(new ArrayList<Integer>(), new ArrayList<Integer>());
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user