year
stringdate
2014-01-01 00:00:00
2024-01-01 00:00:00
session
stringlengths
3
4
utility_classes
listlengths
1
5
solution
listlengths
1
6
test
dict
2024
a01c
[ { "content": "package a01c.sol1;\n\nimport java.util.List;\n\n\npublic interface SimpleIteratorFactory {\n\n \n SimpleIterator<Integer> naturals();\n\n \n <X> SimpleIterator<X> circularFromList(List<X> list);\n\n \n <X> SimpleIterator<X> cut(int size, SimpleIterator<X> simpleIterator);\n\n \n <X> SimpleIterator<Pair<X, X>> window2(SimpleIterator<X> simpleIterator);\n\n \n SimpleIterator<Integer> sumPairs(SimpleIterator<Integer> simpleIterator);\n\n \n <X> SimpleIterator<List<X>> window(int windowSize, SimpleIterator<X> simpleIterator);\n}", "filename": "SimpleIteratorFactory.java" }, { "content": "package a01c.sol1;\n\n\npublic interface SimpleIterator<X> {\n \n \n X next();\n}", "filename": "SimpleIterator.java" }, { "content": "package a01c.sol1;\n\nimport java.util.Objects;\n\n\n\npublic class Pair<E1,E2> {\n\t\n\tprivate final E1 e1;\n\tprivate final E2 e2;\n\t\n\tpublic Pair(E1 x, E2 y) {\n\t\tsuper();\n\t\tthis.e1 = x;\n\t\tthis.e2 = y;\n\t}\n\n\tpublic E1 get1() {\n\t\treturn e1;\n\t}\n\n\tpublic E2 get2() {\n\t\treturn e2;\n\t}\n\n\t@Override\n\tpublic int hashCode() {\n\t\treturn Objects.hash(e1, e2);\n\t}\n\n\t@Override\n\tpublic boolean equals(Object obj) {\n\t\tif (this == obj) {\n\t\t\treturn true;\n\t\t}\n\t\tif (obj == null) {\n\t\t\treturn false;\n\t\t}\n\t\tif (getClass() != obj.getClass()) {\n\t\t\treturn false;\n\t\t}\n\t\tPair other = (Pair) obj;\n\t\treturn Objects.equals(e1, other.e1) && Objects.equals(e2, other.e2);\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Pair [e1=\" + e1 + \", e2=\" + e2 + \"]\";\n\t}\n\t\n\t\n\n}", "filename": "Pair.java" } ]
[ { "content": "package a01c.sol1;\n\nimport java.util.Iterator;\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.stream.Stream;\n\npublic class SimpleIteratorFactoryImpl implements SimpleIteratorFactory {\n\n private static <X> SimpleIterator<X> fromIterator(Iterator<X> iterator){\n return iterator::next;\n }\n\n private static <X> Stream<X> toStream(SimpleIterator<X> simpleIterator){\n return Stream.generate(simpleIterator::next);\n }\n\n @Override\n public SimpleIterator<Integer> naturals() {\n return fromIterator(Stream.iterate(0, i -> i+1).iterator());\n }\n\n @Override\n public <X> SimpleIterator<X> circularFromList(List<X> list) {\n return fromIterator(Stream.iterate(0, i -> i + 1).map(i -> i % list.size()).map(list::get).iterator());\n }\n\n @Override\n public <X> SimpleIterator<X> cut(int size, SimpleIterator<X> simpleIterator) {\n return fromIterator(toStream(simpleIterator).limit(size).iterator());\n }\n\n @Override\n public <X> SimpleIterator<Pair<X, X>> window2(SimpleIterator<X> iterator) {\n return fromIterator(toStream(window(2, iterator)).map(l -> new Pair<>(l.get(0), l.get(1))).iterator());\n }\n\n @Override\n public SimpleIterator<Integer> sumPairs(SimpleIterator<Integer> iterator) {\n return fromIterator(toStream(window2(iterator)).map(p -> p.get1() + p.get2()).iterator());\n }\n\n @Override\n public <X> SimpleIterator<List<X>> window(int windowSize, SimpleIterator<X> iterator) {\n final List<X> list = new LinkedList<>();\n return fromIterator(toStream(iterator)\n .map(e -> {\n list.add(e); \n if (list.size() > windowSize){\n list.remove(0);\n }\n return list;\n })\n .skip(windowSize-1)\n .iterator());\n }\n}", "filename": "SimpleIteratorFactoryImpl.java" } ]
{ "components": { "imports": "package a01c.sol1;\n\nimport static org.junit.Assert.*;\n\nimport java.util.*;", "private_init": "\t/*\n\t * Implement the SimpleIteratorFactory interface as indicated in the initFactory\n\t * method below. Creates a factory for SimpleIterators, which only have one\n\t * method to get the next element (which may fail if the iteration is finished).\n\t * \n\t * The following are considered optional for the possibility of correcting\n\t * the exercise, but still contribute to achieving the totality of the\n\t * score:\n\t * \n\t * - implementation of all factory methods (i.e., in the\n\t * mandatory part it is sufficient to implement all but one at will)\n\t * - good design of the solution, using design solutions that lead to\n\t * succinct code that avoids repetitions and memory waste.\n\t * \n\t * Remove the comment from the initFactory method.\n\t * \n\t * Score indications:\n\t * - correctness of the mandatory part (and absence of code defects): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t */\n\n\tprivate SimpleIteratorFactory factory;\n\n\[email protected]\n\tpublic void initFactory() {\n\t\tthis.factory = new SimpleIteratorFactoryImpl();\n\t}\n\n\t// a utility to facilitate testing, which verifies if the next elements generated by the iterator\n\t// correspond to the expected list\n\tprivate <X> boolean nextElements(List<X> expected, SimpleIterator<X> iterator){\n\t\treturn expected.stream().allMatch(e -> e.equals(iterator.next()));\n\t}", "test_functions": [ "\[email protected]\n\tpublic void testNaturals(){\n\t\t// naturals produce 0,1,2,3,...\n\t\tvar it = this.factory.naturals();\n\t\tassertEquals(0, it.next().intValue());\n\t\tassertEquals(1, it.next().intValue());\n\t\tassertEquals(2, it.next().intValue());\n\t\tassertEquals(3, it.next().intValue());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}", "\[email protected]\n\tpublic void testCircularList(){\n\t\t// produce \"a\", \"b\", \"c\", \"a\",... indefinitely\n\t\tvar it = this.factory.circularFromList(List.of(\"a\",\"b\",\"c\"));\n\t\tassertTrue(nextElements(List.of(\"a\",\"b\",\"c\",\"a\",\"b\",\"c\",\"a\"), it));\n\t}", "\[email protected]\n\tpublic void testCut(){\n\t\t// after the first 5 elements, each call to next fails\n\t\tvar it = this.factory.cut(5, this.factory.naturals());\n\t\tassertEquals(0, it.next().intValue());\n\t\tassertEquals(1, it.next().intValue());\n\t\tassertEquals(2, it.next().intValue());\n\t\tassertEquals(3, it.next().intValue());\n\t\tassertEquals(4, it.next().intValue());\n\t\tassertThrows(NoSuchElementException.class, () -> it.next());\n\t\tassertThrows(NoSuchElementException.class, () -> it.next());\n\t}", "\[email protected]\n\tpublic void testWindow2(){\n\t\t// produce successive pairs starting from the input iterator, here naturals\n\t\tvar it = this.factory.window2(this.factory.naturals());\n\t\tassertEquals(new Pair<>(0, 1), it.next());\n\t\tassertEquals(new Pair<>(1, 2), it.next());\n\t\tassertEquals(new Pair<>(2, 3), it.next());\n\t\tassertEquals(new Pair<>(3, 4), it.next());\n\t\tassertEquals(new Pair<>(4, 5), it.next());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}", "\[email protected]\n\tpublic void testSums(){\n\t\t// produces the sum of successive pairs, indefinitely\n\t\tvar it = this.factory.sumPairs(this.factory.circularFromList(List.of(10,20,30,40)));\n\t\tassertEquals(10+20, it.next().intValue());\n\t\tassertEquals(20+30, it.next().intValue());\n\t\tassertEquals(30+40, it.next().intValue());\n\t\tassertEquals(40+10, it.next().intValue());\n\t\tassertEquals(10+20, it.next().intValue());\n\t}", "\[email protected]\n\tpublic void testWindow(){\n\t\t// generalizes window2, but producing lists of n elements, 3 in this case\n\t\tvar it = this.factory.window(3, this.factory.circularFromList(List.of(10,20,30,40)));\n\t\tassertEquals(List.of(10,20,30), it.next());\n\t\tassertEquals(List.of(20,30,40), it.next());\n\t\tassertEquals(List.of(30,40,10), it.next());\n\t\tassertEquals(List.of(40,10,20), it.next());\n\t\tassertEquals(List.of(10,20,30), it.next());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}" ] }, "content": "package a01c.sol1;\n\nimport static org.junit.Assert.*;\n\nimport java.util.*;\n\npublic class Test {\n\n\t/*\n\t * Implement the SimpleIteratorFactory interface as indicated in the initFactory\n\t * method below. Creates a factory for SimpleIterators, which only have one\n\t * method to get the next element (which may fail if the iteration is finished).\n\t * \n\t * The following are considered optional for the possibility of correcting\n\t * the exercise, but still contribute to achieving the totality of the\n\t * score:\n\t * \n\t * - implementation of all factory methods (i.e., in the\n\t * mandatory part it is sufficient to implement all but one at will)\n\t * - good design of the solution, using design solutions that lead to\n\t * succinct code that avoids repetitions and memory waste.\n\t * \n\t * Remove the comment from the initFactory method.\n\t * \n\t * Score indications:\n\t * - correctness of the mandatory part (and absence of code defects): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t */\n\n\tprivate SimpleIteratorFactory factory;\n\n\[email protected]\n\tpublic void initFactory() {\n\t\tthis.factory = new SimpleIteratorFactoryImpl();\n\t}\n\n\t// a utility to facilitate testing, which verifies if the next elements generated by the iterator\n\t// correspond to the expected list\n\tprivate <X> boolean nextElements(List<X> expected, SimpleIterator<X> iterator){\n\t\treturn expected.stream().allMatch(e -> e.equals(iterator.next()));\n\t}\n\t\n\[email protected]\n\tpublic void testNaturals(){\n\t\t// naturals produce 0,1,2,3,...\n\t\tvar it = this.factory.naturals();\n\t\tassertEquals(0, it.next().intValue());\n\t\tassertEquals(1, it.next().intValue());\n\t\tassertEquals(2, it.next().intValue());\n\t\tassertEquals(3, it.next().intValue());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}\n\t\tassertEquals(104, it.next().intValue());\n\t\tassertTrue(nextElements(List.of(105, 106, 107), it));\n\t}\n\n\[email protected]\n\tpublic void testCircularList(){\n\t\t// produce \"a\", \"b\", \"c\", \"a\",... indefinitely\n\t\tvar it = this.factory.circularFromList(List.of(\"a\",\"b\",\"c\"));\n\t\tassertTrue(nextElements(List.of(\"a\",\"b\",\"c\",\"a\",\"b\",\"c\",\"a\"), it));\n\t}\n\n\[email protected]\n\tpublic void testCut(){\n\t\t// after the first 5 elements, each call to next fails\n\t\tvar it = this.factory.cut(5, this.factory.naturals());\n\t\tassertEquals(0, it.next().intValue());\n\t\tassertEquals(1, it.next().intValue());\n\t\tassertEquals(2, it.next().intValue());\n\t\tassertEquals(3, it.next().intValue());\n\t\tassertEquals(4, it.next().intValue());\n\t\tassertThrows(NoSuchElementException.class, () -> it.next());\n\t\tassertThrows(NoSuchElementException.class, () -> it.next());\n\t}\n\n\[email protected]\n\tpublic void testWindow2(){\n\t\t// produce successive pairs starting from the input iterator, here naturals\n\t\tvar it = this.factory.window2(this.factory.naturals());\n\t\tassertEquals(new Pair<>(0, 1), it.next());\n\t\tassertEquals(new Pair<>(1, 2), it.next());\n\t\tassertEquals(new Pair<>(2, 3), it.next());\n\t\tassertEquals(new Pair<>(3, 4), it.next());\n\t\tassertEquals(new Pair<>(4, 5), it.next());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}\n\t}\n\n\[email protected]\n\tpublic void testSums(){\n\t\t// produces the sum of successive pairs, indefinitely\n\t\tvar it = this.factory.sumPairs(this.factory.circularFromList(List.of(10,20,30,40)));\n\t\tassertEquals(10+20, it.next().intValue());\n\t\tassertEquals(20+30, it.next().intValue());\n\t\tassertEquals(30+40, it.next().intValue());\n\t\tassertEquals(40+10, it.next().intValue());\n\t\tassertEquals(10+20, it.next().intValue());\n\t}\n\n\[email protected]\n\tpublic void testWindow(){\n\t\t// generalizes window2, but producing lists of n elements, 3 in this case\n\t\tvar it = this.factory.window(3, this.factory.circularFromList(List.of(10,20,30,40)));\n\t\tassertEquals(List.of(10,20,30), it.next());\n\t\tassertEquals(List.of(20,30,40), it.next());\n\t\tassertEquals(List.of(30,40,10), it.next());\n\t\tassertEquals(List.of(40,10,20), it.next());\n\t\tassertEquals(List.of(10,20,30), it.next());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}\n\n\t\t// iterator on windows of 5 elements starting from naturals\n\t\tit = this.factory.window(5, this.factory.naturals());\n\t\tassertEquals(List.of(0,1,2,3,4), it.next());\n\t\tassertEquals(List.of(1,2,3,4,5), it.next());\n\t\tfor (int i = 0; i < 100; i++){\n\t\t\tit.next();\n\t\t}\n\t\tassertEquals(List.of(102,103,104,105,106), it.next());\n\t}\n}", "filename": "Test.java" }
2024
a03b
[ { "content": "package a03b.sol1;\n\nimport java.util.List;\n\n\npublic interface DependencyArray<E> {\n\n \n int size();\n\n \n E read(int position);\n\n \n void write(int position, E value);\n\n \n List<E> elements();\n \n}", "filename": "DependencyArray.java" }, { "content": "package a03b.sol1;\n\nimport java.util.List;\n\n\npublic interface DependencyArrayFactory {\n\n \n <E> DependencyArray<E> immutable(List<E> initial);\n\n \n <E> DependencyArray<E> mutable(List<E> initial);\n\n \n DependencyArray<Integer> withSumOfElementsAtTheEnd(List<Integer> initial);\n\n \n <E> DependencyArray<E> clonedWithOneRandom(DependencyArray<E> array, int pos, List<E> randomElements);\n\n \n DependencyArray<Integer> clonedWithAddedProduct(DependencyArray<Integer> array);\n}", "filename": "DependencyArrayFactory.java" }, { "content": "package a03b.sol1;\n\nimport java.util.Objects;\n\n\n\npublic class Pair<E1,E2> {\n\t\n\tprivate final E1 e1;\n\tprivate final E2 e2;\n\t\n\tpublic Pair(E1 x, E2 y) {\n\t\tsuper();\n\t\tthis.e1 = x;\n\t\tthis.e2 = y;\n\t}\n\n\tpublic E1 get1() {\n\t\treturn e1;\n\t}\n\n\tpublic E2 get2() {\n\t\treturn e2;\n\t}\n\n\t@Override\n\tpublic int hashCode() {\n\t\treturn Objects.hash(e1, e2);\n\t}\n\n\t@Override\n\tpublic boolean equals(Object obj) {\n\t\tif (this == obj) {\n\t\t\treturn true;\n\t\t}\n\t\tif (obj == null) {\n\t\t\treturn false;\n\t\t}\n\t\tif (getClass() != obj.getClass()) {\n\t\t\treturn false;\n\t\t}\n\t\tPair other = (Pair) obj;\n\t\treturn Objects.equals(e1, other.e1) && Objects.equals(e2, other.e2);\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Pair [e1=\" + e1 + \", e2=\" + e2 + \"]\";\n\t}\n\t\n\t\n\n}", "filename": "Pair.java" } ]
[ { "content": "package a03b.sol1;\n\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.Random;\nimport java.util.function.BinaryOperator;\nimport java.util.function.IntFunction;\nimport java.util.stream.IntStream;\n\npublic class DependencyArrayFactoryImpl implements DependencyArrayFactory {\n\n private static class DependencyArrayImpl<E> implements DependencyArray<E> {\n private final List<E> initialElements;\n private final IntFunction<Boolean> flags;\n\n private DependencyArrayImpl(IntFunction<Boolean> flags, List<E> initialElements) {\n this.initialElements = new LinkedList<>(initialElements);\n this.flags = flags;\n }\n\n @Override\n public int size() {\n return this.initialElements.size();\n }\n\n @Override\n public E read(int position) {\n return initialElements.get(position);\n }\n\n @Override\n public void write(int position, E value) {\n if (!flags.apply(position)) {\n throw new UnsupportedOperationException();\n }\n this.initialElements.set(position, value);\n }\n\n @Override\n public List<E> elements() {\n return IntStream.range(0, size()).mapToObj(this::read).toList();\n }\n }\n\n @Override\n public <E> DependencyArray<E> immutable(List<E> initial) {\n return new DependencyArrayImpl<>(pos -> false, initial);\n }\n\n @Override\n public <E> DependencyArray<E> mutable(List<E> initial) {\n return new DependencyArrayImpl<>(pos -> true, initial);\n }\n\n @Override\n public DependencyArray<Integer> withSumOfElementsAtTheEnd(List<Integer> initial) {\n return withAddedReduction(mutable(initial), 0, (x, y) -> x + y);\n }\n\n @Override\n public <E> DependencyArray<E> clonedWithOneRandom(DependencyArray<E> array, int pos, List<E> randomElements) {\n var random = new Random();\n return new DependencyArrayImpl<>(p -> p != pos, array.elements()) {\n @Override\n public E read(int position) {\n if (position == pos) {\n return randomElements.get(random.nextInt(size()));\n }\n return super.read(position);\n }\n };\n }\n\n private <E> DependencyArray<E> withAddedReduction(DependencyArray<E> array, E base, BinaryOperator<E> op) {\n return new DependencyArrayImpl<>(p -> p != array.size(), array.elements()) {\n\n @Override\n public int size() {\n return array.size() + 1;\n }\n\n @Override\n public E read(int position) {\n if (position == array.size()) {\n return IntStream.range(0, size()-1).mapToObj(this::read).reduce(base, op);\n }\n return super.read(position);\n }\n\n };\n\n }\n\n @Override\n public DependencyArray<Integer> clonedWithAddedProduct(DependencyArray<Integer> array) {\n return withAddedReduction(array, 1, (x, y) -> x * y);\n }\n\n}", "filename": "DependencyArrayFactoryImpl.java" } ]
{ "components": { "imports": "package a03b.sol1;\n\nimport static org.junit.Assert.*;\nimport java.util.*;", "private_init": "\t/*\n\t * Implement the DependencyArrayFactory interface as indicated in the init method below.\n\t * Create a factory for DependencyArrays which are arrays in which some of its elements\n\t * might not be writable, and/or might be constrained to have a value that depends\n\t * on that of others, for example the last element could always be the sum of the previous ones. Please\n\t * read the comments in the provided interface and especially the tests below for details.\n\t *\n\t * The following are considered optional for the purpose of being able to correct\n\t * the exercise, but still contribute to achieving the totality of the\n\t * score:\n\t *\n\t * - Make all tests pass (i.e., in the mandatory part it is sufficient\n\t * that all tests below pass except for one of your choice)\n\t * - The good design of the solution, using design solutions that lead to\n\t * concise code that avoids repetition and memory waste.\n\t *\n\t * Remove the comment from the init method.\n\t *\n\t * Scoring indications:\n\t * - correctness of the mandatory part (and absence of code defects): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t */\n\n\tprivate DependencyArrayFactory factory;\n\n\[email protected]\n\tpublic void init() {\n\t\tthis.factory = new DependencyArrayFactoryImpl();\n\t}", "test_functions": [ "\[email protected]\n\tpublic void testImmutable() {\n\t\t// a DependencyArray that corresponds to a simple immutable array\n\t\tvar array = this.factory.immutable(List.of(10,20,30));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(0, 100));\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(1, 100));\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(2, 100));\n\t\tassertEquals(List.of(10,20,30), array.elements());\n\t}", "\[email protected]\n\tpublic void testMutable() {\n\t\t// a DependencyArray that corresponds to a simple mutable array\n\t\tvar array = this.factory.mutable(List.of(10,20,30));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertEquals(List.of(10,20,30), array.elements());\n\t\tarray.write(0, 11);\n\t\tassertEquals(11, array.read(0).intValue());\n\t\tassertEquals(List.of(11,20,30), array.elements());\n\t\tarray.write(2, 31);\n\t\tassertEquals(List.of(11,20,31), array.elements());\n\t}", "\[email protected]\n\tpublic void testWithSomeOfElementsAtTheEnd() {\n\t\t// a mutable DependencyArray, but which at the end has an element that is the sum of the previous ones\n\t\tvar array = this.factory.withSumOfElementsAtTheEnd(List.of(10,20,30));\n\t\tassertEquals(4, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertEquals(60, array.read(3).intValue()); // sum of the previous ones\n\t\tassertEquals(List.of(10,20,30, 60), array.elements());\n\t\t// I change the first one, the last one is automatically changed, and so on...\n\t\tarray.write(0, 11);\n\t\tassertEquals(List.of(11,20,30, 61), array.elements());\n\t\tassertEquals(11, array.read(0).intValue());\n\t\tarray.write(2, 31);\n\t\tassertEquals(List.of(11,20,31,62), array.elements());\n\t\t// the last one, i.e. the sum, is not modifiable\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(3, 0));\n\t}", "\[email protected]\n\tpublic void testChangeOneToRandom() {\n\t\tvar start = this.factory.mutable(List.of(\"10\",\"20\",\"30\"));\n\t\t// a DependencyArray similar to start, but where in position 1 a random value is read, different each time, taken from \"19\",\"20\",\"21\"\n\t\tvar array = this.factory.clonedWithOneRandom(start, 1, List.of(\"19\", \"20\", \"21\"));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(\"10\", array.read(0));\n\t\tassertEquals(\"30\", array.read(2));\n\t\t// reading 100 times from position 1, it will happen to read \"19\", \"20\", \"21\"\n\t\tSet<String> randomResults = new HashSet<>();\n\t\tfor (int i=0; i<100; i++){\n\t\t\trandomResults.add(array.read(1));\n\t\t}", "\[email protected]\n\tpublic void testWithAddedProduct() {\n\t\tvar start = this.factory.mutable(List.of(10, 2 ,3));\n\t\t// a DependencyArray similar to start, but where an element is added at the end that is the product of the previous ones\n\t\tvar array = this.factory.clonedWithAddedProduct(start);\n\t\tassertEquals(4, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(2, array.read(1).intValue());\n\t\tassertEquals(3, array.read(2).intValue());\n\t\tassertEquals(60, array.read(3).intValue());\n\t\tassertEquals(List.of(10,2,3, 60), array.elements());\n\t\tarray.write(0, -10);\n\t\tassertEquals(List.of(-10,2,3, -60), array.elements());\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(3, 0));\n\t\t// I reapply the method creating a new array, array2\n\t\tvar array2 = this.factory.clonedWithAddedProduct(array);\n\t\tassertEquals(List.of(-10,2,3, -60, 3600), array2.elements());\n\t}" ] }, "content": "package a03b.sol1;\n\nimport static org.junit.Assert.*;\nimport java.util.*;\n\npublic class Test {\n\n\t/*\n\t * Implement the DependencyArrayFactory interface as indicated in the init method below.\n\t * Create a factory for DependencyArrays which are arrays in which some of its elements\n\t * might not be writable, and/or might be constrained to have a value that depends\n\t * on that of others, for example the last element could always be the sum of the previous ones. Please\n\t * read the comments in the provided interface and especially the tests below for details.\n\t *\n\t * The following are considered optional for the purpose of being able to correct\n\t * the exercise, but still contribute to achieving the totality of the\n\t * score:\n\t *\n\t * - Make all tests pass (i.e., in the mandatory part it is sufficient\n\t * that all tests below pass except for one of your choice)\n\t * - The good design of the solution, using design solutions that lead to\n\t * concise code that avoids repetition and memory waste.\n\t *\n\t * Remove the comment from the init method.\n\t *\n\t * Scoring indications:\n\t * - correctness of the mandatory part (and absence of code defects): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t */\n\n\tprivate DependencyArrayFactory factory;\n\n\[email protected]\n\tpublic void init() {\n\t\tthis.factory = new DependencyArrayFactoryImpl();\n\t}\n\n\[email protected]\n\tpublic void testImmutable() {\n\t\t// a DependencyArray that corresponds to a simple immutable array\n\t\tvar array = this.factory.immutable(List.of(10,20,30));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(0, 100));\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(1, 100));\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(2, 100));\n\t\tassertEquals(List.of(10,20,30), array.elements());\n\t}\n\n\[email protected]\n\tpublic void testMutable() {\n\t\t// a DependencyArray that corresponds to a simple mutable array\n\t\tvar array = this.factory.mutable(List.of(10,20,30));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertEquals(List.of(10,20,30), array.elements());\n\t\tarray.write(0, 11);\n\t\tassertEquals(11, array.read(0).intValue());\n\t\tassertEquals(List.of(11,20,30), array.elements());\n\t\tarray.write(2, 31);\n\t\tassertEquals(List.of(11,20,31), array.elements());\n\t}\n\n\[email protected]\n\tpublic void testWithSomeOfElementsAtTheEnd() {\n\t\t// a mutable DependencyArray, but which at the end has an element that is the sum of the previous ones\n\t\tvar array = this.factory.withSumOfElementsAtTheEnd(List.of(10,20,30));\n\t\tassertEquals(4, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(20, array.read(1).intValue());\n\t\tassertEquals(30, array.read(2).intValue());\n\t\tassertEquals(60, array.read(3).intValue()); // sum of the previous ones\n\t\tassertEquals(List.of(10,20,30, 60), array.elements());\n\t\t// I change the first one, the last one is automatically changed, and so on...\n\t\tarray.write(0, 11);\n\t\tassertEquals(List.of(11,20,30, 61), array.elements());\n\t\tassertEquals(11, array.read(0).intValue());\n\t\tarray.write(2, 31);\n\t\tassertEquals(List.of(11,20,31,62), array.elements());\n\t\t// the last one, i.e. the sum, is not modifiable\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(3, 0));\n\t}\n\n\[email protected]\n\tpublic void testChangeOneToRandom() {\n\t\tvar start = this.factory.mutable(List.of(\"10\",\"20\",\"30\"));\n\t\t// a DependencyArray similar to start, but where in position 1 a random value is read, different each time, taken from \"19\",\"20\",\"21\"\n\t\tvar array = this.factory.clonedWithOneRandom(start, 1, List.of(\"19\", \"20\", \"21\"));\n\t\tassertEquals(3, array.size());\n\t\tassertEquals(\"10\", array.read(0));\n\t\tassertEquals(\"30\", array.read(2));\n\t\t// reading 100 times from position 1, it will happen to read \"19\", \"20\", \"21\"\n\t\tSet<String> randomResults = new HashSet<>();\n\t\tfor (int i=0; i<100; i++){\n\t\t\trandomResults.add(array.read(1));\n\t\t}\n\t\tassertEquals(Set.of(\"19\", \"20\", \"21\"), randomResults);\n\t\t// position 0 (and 2) are writable\n\t\tarray.write(0, \"9\");\n\t\tvar list = array.elements();\n\t\tassertEquals(3, list.size());\n\t\tassertEquals(\"9\", list.get(0));\n\t\tassertEquals(\"30\", list.get(2));\n\t\tassertTrue(Set.of(\"19\", \"20\", \"21\").contains(list.get(1)));\n\t\t// position 1 is not writable\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(1, \"0\"));\n\t}\n\n\[email protected]\n\tpublic void testWithAddedProduct() {\n\t\tvar start = this.factory.mutable(List.of(10, 2 ,3));\n\t\t// a DependencyArray similar to start, but where an element is added at the end that is the product of the previous ones\n\t\tvar array = this.factory.clonedWithAddedProduct(start);\n\t\tassertEquals(4, array.size());\n\t\tassertEquals(10, array.read(0).intValue());\n\t\tassertEquals(2, array.read(1).intValue());\n\t\tassertEquals(3, array.read(2).intValue());\n\t\tassertEquals(60, array.read(3).intValue());\n\t\tassertEquals(List.of(10,2,3, 60), array.elements());\n\t\tarray.write(0, -10);\n\t\tassertEquals(List.of(-10,2,3, -60), array.elements());\n\t\tassertThrows(UnsupportedOperationException.class, () -> array.write(3, 0));\n\t\t// I reapply the method creating a new array, array2\n\t\tvar array2 = this.factory.clonedWithAddedProduct(array);\n\t\tassertEquals(List.of(-10,2,3, -60, 3600), array2.elements());\n\t}\n}", "filename": "Test.java" }
2024
a01b
[ { "content": "package a01b.sol1;\n\nimport java.util.Iterator;\nimport java.util.function.BinaryOperator;\n\n\npublic interface IteratorsCombiners {\n\n \n <X> Iterator<X> alternate(Iterator<X> i1, Iterator<X> i2);\n\n \n <X> Iterator<X> seq(Iterator<X> i1, Iterator<X> i2);\n\n \n <X> Iterator<X> map2(Iterator<X> i1, Iterator<X> i2, BinaryOperator<X> operator);\n\n \n <X, Y, Z> Iterator<Pair<X, Y>> zip(Iterator<X> i1, Iterator<Y> i2);\n}", "filename": "IteratorsCombiners.java" }, { "content": "package a01b.sol1;\n\nimport java.util.Objects;\n\n\n\npublic class Pair<E1,E2> {\n\t\n\tprivate final E1 e1;\n\tprivate final E2 e2;\n\t\n\tpublic Pair(E1 x, E2 y) {\n\t\tsuper();\n\t\tthis.e1 = x;\n\t\tthis.e2 = y;\n\t}\n\n\tpublic E1 get1() {\n\t\treturn e1;\n\t}\n\n\tpublic E2 get2() {\n\t\treturn e2;\n\t}\n\n\t@Override\n\tpublic int hashCode() {\n\t\treturn Objects.hash(e1, e2);\n\t}\n\n\t@Override\n\tpublic boolean equals(Object obj) {\n\t\tif (this == obj) {\n\t\t\treturn true;\n\t\t}\n\t\tif (obj == null) {\n\t\t\treturn false;\n\t\t}\n\t\tif (getClass() != obj.getClass()) {\n\t\t\treturn false;\n\t\t}\n\t\tPair other = (Pair) obj;\n\t\treturn Objects.equals(e1, other.e1) && Objects.equals(e2, other.e2);\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Pair [e1=\" + e1 + \", e2=\" + e2 + \"]\";\n\t}\n\t\n\t\n\n}", "filename": "Pair.java" } ]
[ { "content": "package a01b.sol1;\n\nimport java.util.Collections;\nimport java.util.Iterator;\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.function.BiFunction;\nimport java.util.function.BinaryOperator;\nimport java.util.stream.Stream;\n\npublic class IteratorsCombinersFactory implements IteratorsCombiners {\n\n private <X, Y, Z> Iterator<Z> genericCombiner(Iterator<X> i1, Iterator<Y> i2,\n BiFunction<Iterator<X>, Iterator<Y>, List<Z>> function) {\n return new Iterator<>() {\n List<Z> cache = new LinkedList<>();\n\n @Override\n public boolean hasNext() {\n checkCache();\n return !cache.isEmpty();\n }\n\n private void checkCache() {\n if (cache.isEmpty()) {\n cache.addAll(function.apply(i1,i2));\n }\n }\n\n @Override\n public Z next() {\n checkCache();\n return cache.remove(0);\n }\n };\n }\n\n @Override\n public <X> Iterator<X> alternate(Iterator<X> i1, Iterator<X> i2) {\n return genericCombiner(i1, i2, (it1, it2) -> Stream.of(it1, it2).filter(Iterator::hasNext).map(Iterator::next).toList());\n }\n \n @Override\n public <X> Iterator<X> seq(Iterator<X> i1, Iterator<X> i2) {\n return genericCombiner(i1, i2, (it1, it2) -> it1.hasNext() ? List.of(it1.next()) \n : it2.hasNext() ? List.of(it2.next()) : List.of());\n }\n\n private <X, Y, Z> Iterator<Z> genericMap2(Iterator<X> i1, Iterator<Y> i2, BiFunction<X,Y,Z> function){\n return genericCombiner(i1, i2, (it1, it2) -> it1.hasNext() && it2.hasNext() ? \n List.of(function.apply(it1.next(), i2.next())) : Collections.emptyList());\n }\n\n @Override\n public <X> Iterator<X> map2(Iterator<X> i1, Iterator<X> i2, BinaryOperator<X> operator){\n return genericMap2(i1, i2, operator);\n }\n\n @Override\n public <X, Y, Z> Iterator<Pair<X,Y>> zip(Iterator<X> i1, Iterator<Y> i2){\n return genericMap2(i1, i2, Pair::new);\n }\n \n\n}", "filename": "IteratorsCombinersFactory.java" } ]
{ "components": { "imports": "package a01b.sol1;\n\nimport static org.junit.Assert.*;\n\nimport java.util.*;\nimport java.util.stream.Stream;", "private_init": "\t/*\n\t * Implement the IteratorCombiners interface as indicated in the initFactory method below.\n\t * Create a factory to create standard iterators (java.util.Iterator) by combining two existing iterators.\n\t *\n\t * The following are considered optional for the possibility of correcting the exercise,\n\t * but still contribute to achieving the totality of the score:\n\t *\n\t * - implementation of all methods of the factory (i.e., in the mandatory part it is sufficient to implement all except one of your choice)\n\t * - the good design of the solution, using design solutions that lead to concise code that avoids repetitions and memory waste\n\t *\n\t * Remove the comment from the initFactory method.\n\t *\n\t * Scoring indications:\n\t * - correctness of the mandatory part (and absence of defects in the code): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t *\n\t */\n\n\tprivate IteratorsCombiners factory;\n\n\[email protected]\n\tpublic void initFactory() {\n\t\tthis.factory = new IteratorsCombinersFactory();\n\t}\n\n\t// a utility to facilitate testing, which converts iterators into lists\n\tprivate <X> List<X> toList(Iterator<X> iterator){\n\t\treturn Stream.iterate(iterator, Iterator::hasNext, it -> it).map(Iterator::next).toList();\n\t}", "test_functions": [ "\[email protected]\n\tpublic void testAlternate() {\n\t\t// alternating between the iterator that produces (10,20,30) and the one that produces (40,50)\n\t\t// results in an iterator that produces (10,40,20,50,30), and similarly for the other cases...\n\t\tassertEquals(List.of(10,40,20,50,30), \n\t\t\ttoList(this.factory.alternate(List.of(10,20,30).iterator(), List.of(40,50).iterator())));\n\t\tassertEquals(List.of(10,40,20,30), \n\t\t\ttoList(this.factory.alternate(List.of(10,20,30).iterator(), List.of(40).iterator())));\n\t\tassertEquals(List.of(10,40,50,60), \n\t\t\ttoList(this.factory.alternate(List.of(10).iterator(), List.of(40,50,60).iterator())));\n\t}", "\[email protected]\n\tpublic void testSequence() {\n\t\t// sequencing the iterator that produces (10,20,30) and the one that produces (40,50)\n\t\t// results in an iterator that produces (10,20,30,40,50), and similarly for the other cases...\n\t\tassertEquals(List.of(10,20,30,40,50), \n\t\t\ttoList(this.factory.seq(List.of(10,20,30).iterator(), List.of(40,50).iterator())));\n\t\tassertEquals(List.of(10,20,30,40), \n\t\t\ttoList(this.factory.seq(List.of(10,20,30).iterator(), List.of(40).iterator())));\n\t\tassertEquals(List.of(10,40,50,60), \n\t\t\ttoList(this.factory.seq(List.of(10).iterator(), List.of(40,50,60).iterator())));\n\t}", "\[email protected]\n\tpublic void testMap2() {\n\t\t// summing (with map2) the results of the iterators that produce (10,20,30) and (100,200)\n\t\t// results in the iterator that produces (10+100, 20+200) -- and ignores the 30. Similarly for the other cases...\n\t\tassertEquals(List.of(110,220), \n\t\t\ttoList(this.factory.map2(List.of(10,20,30).iterator(), List.of(100,200).iterator(), (x,y)->x+y)));\n\t\tassertEquals(List.of(110), \n\t\t\ttoList(this.factory.map2(List.of(10,20,30).iterator(), List.of(100).iterator(), (x,y)->x+y)));\n\t\tassertEquals(List.of(110), \n\t\t\ttoList(this.factory.map2(List.of(10).iterator(), List.of(100,200,300).iterator(), (x,y)->x+y)));\n\t}", "\[email protected]\n\tpublic void testZip() {\n\t\t// zipping the results of the iterators that produce (10,20,30) and (100,200)\n\t\t// results in the iterator that produces the pairs (<10,100>, <20,200>) -- and ignores the 30. Similarly for the other cases...\n\t\tassertEquals(List.of(new Pair<>(10,100), new Pair<>(20,200)), \n\t\t\ttoList(this.factory.zip(List.of(10,20,30).iterator(), List.of(100,200).iterator())));\n\t\tassertEquals(List.of(new Pair<>(10,100)), \n\t\t\ttoList(this.factory.zip(List.of(10,20,30).iterator(), List.of(100).iterator())));\n\t\tassertEquals(List.of(new Pair<>(10,100)), \n\t\t\ttoList(this.factory.zip(List.of(10).iterator(), List.of(100,200,300).iterator())));\n\t}" ] }, "content": "package a01b.sol1;\n\nimport static org.junit.Assert.*;\n\nimport java.util.*;\nimport java.util.stream.Stream;\n\npublic class Test {\n\n\t/*\n\t * Implement the IteratorCombiners interface as indicated in the initFactory method below.\n\t * Create a factory to create standard iterators (java.util.Iterator) by combining two existing iterators.\n\t *\n\t * The following are considered optional for the possibility of correcting the exercise,\n\t * but still contribute to achieving the totality of the score:\n\t *\n\t * - implementation of all methods of the factory (i.e., in the mandatory part it is sufficient to implement all except one of your choice)\n\t * - the good design of the solution, using design solutions that lead to concise code that avoids repetitions and memory waste\n\t *\n\t * Remove the comment from the initFactory method.\n\t *\n\t * Scoring indications:\n\t * - correctness of the mandatory part (and absence of defects in the code): 10 points\n\t * - correctness of the optional part: 3 points (additional factory method)\n\t * - quality of the solution: 4 points (for good design)\n\t *\n\t */\n\n\tprivate IteratorsCombiners factory;\n\n\[email protected]\n\tpublic void initFactory() {\n\t\tthis.factory = new IteratorsCombinersFactory();\n\t}\n\n\t// a utility to facilitate testing, which converts iterators into lists\n\tprivate <X> List<X> toList(Iterator<X> iterator){\n\t\treturn Stream.iterate(iterator, Iterator::hasNext, it -> it).map(Iterator::next).toList();\n\t}\n\n\[email protected]\n\tpublic void testAlternate() {\n\t\t// alternating between the iterator that produces (10,20,30) and the one that produces (40,50)\n\t\t// results in an iterator that produces (10,40,20,50,30), and similarly for the other cases...\n\t\tassertEquals(List.of(10,40,20,50,30), \n\t\t\ttoList(this.factory.alternate(List.of(10,20,30).iterator(), List.of(40,50).iterator())));\n\t\tassertEquals(List.of(10,40,20,30), \n\t\t\ttoList(this.factory.alternate(List.of(10,20,30).iterator(), List.of(40).iterator())));\n\t\tassertEquals(List.of(10,40,50,60), \n\t\t\ttoList(this.factory.alternate(List.of(10).iterator(), List.of(40,50,60).iterator())));\n\t}\n\n\[email protected]\n\tpublic void testSequence() {\n\t\t// sequencing the iterator that produces (10,20,30) and the one that produces (40,50)\n\t\t// results in an iterator that produces (10,20,30,40,50), and similarly for the other cases...\n\t\tassertEquals(List.of(10,20,30,40,50), \n\t\t\ttoList(this.factory.seq(List.of(10,20,30).iterator(), List.of(40,50).iterator())));\n\t\tassertEquals(List.of(10,20,30,40), \n\t\t\ttoList(this.factory.seq(List.of(10,20,30).iterator(), List.of(40).iterator())));\n\t\tassertEquals(List.of(10,40,50,60), \n\t\t\ttoList(this.factory.seq(List.of(10).iterator(), List.of(40,50,60).iterator())));\n\t}\n\n\[email protected]\n\tpublic void testMap2() {\n\t\t// summing (with map2) the results of the iterators that produce (10,20,30) and (100,200)\n\t\t// results in the iterator that produces (10+100, 20+200) -- and ignores the 30. Similarly for the other cases...\n\t\tassertEquals(List.of(110,220), \n\t\t\ttoList(this.factory.map2(List.of(10,20,30).iterator(), List.of(100,200).iterator(), (x,y)->x+y)));\n\t\tassertEquals(List.of(110), \n\t\t\ttoList(this.factory.map2(List.of(10,20,30).iterator(), List.of(100).iterator(), (x,y)->x+y)));\n\t\tassertEquals(List.of(110), \n\t\t\ttoList(this.factory.map2(List.of(10).iterator(), List.of(100,200,300).iterator(), (x,y)->x+y)));\n\t}\n\n\[email protected]\n\tpublic void testZip() {\n\t\t// zipping the results of the iterators that produce (10,20,30) and (100,200)\n\t\t// results in the iterator that produces the pairs (<10,100>, <20,200>) -- and ignores the 30. Similarly for the other cases...\n\t\tassertEquals(List.of(new Pair<>(10,100), new Pair<>(20,200)), \n\t\t\ttoList(this.factory.zip(List.of(10,20,30).iterator(), List.of(100,200).iterator())));\n\t\tassertEquals(List.of(new Pair<>(10,100)), \n\t\t\ttoList(this.factory.zip(List.of(10,20,30).iterator(), List.of(100).iterator())));\n\t\tassertEquals(List.of(new Pair<>(10,100)), \n\t\t\ttoList(this.factory.zip(List.of(10).iterator(), List.of(100,200,300).iterator())));\n\t}\n}", "filename": "Test.java" }