Просмотр исходного кода

list-ops: add to track (#207)


* Added list-ops exercise to the Java track
* Removed NetBeans settings from the project dir
* Added collect() operation and tests
Dmitry Noranovich 9 лет назад
Родитель
Сommit
41fe578fe2

+ 5
- 0
config.json Просмотреть файл

303
       "slug": "change",
303
       "slug": "change",
304
       "difficulty": 1,
304
       "difficulty": 1,
305
       "topics": []
305
       "topics": []
306
+    },
307
+    {
308
+      "slug": "list-ops",
309
+      "difficulty": 1,
310
+      "topics": []
306
     }
311
     }
307
   ],
312
   ],
308
   "deprecated": [
313
   "deprecated": [

+ 18
- 0
exercises/list-ops/build.gradle Просмотреть файл

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+
13
+test {
14
+    testLogging {
15
+        exceptionFormat = 'full'
16
+        events = ["passed", "failed", "skipped"]
17
+    }
18
+}

+ 51
- 0
exercises/list-ops/src/example/java/ListOps.java Просмотреть файл

1
+
2
+import java.util.ArrayList;
3
+import java.util.Collection;
4
+import java.util.Collections;
5
+import java.util.List;
6
+import java.util.function.BiFunction;
7
+import java.util.function.BinaryOperator;
8
+import java.util.function.Predicate;
9
+import java.util.function.UnaryOperator;
10
+import java.util.stream.Collectors;
11
+import java.util.stream.Stream;
12
+
13
+public class ListOps {
14
+
15
+    private ListOps() {
16
+    }
17
+
18
+    public static <T> int length(final List<T> list) {
19
+        return list.size();
20
+    }
21
+
22
+    public static <T> List<T> reverse(final List<T> list) {
23
+        List<T> result = new ArrayList(list);
24
+        Collections.reverse(result);
25
+        return result;
26
+    }
27
+
28
+    public static <T> List<T> map(final List<T> list,
29
+            UnaryOperator<T> mapper) {
30
+        return list.stream().map(mapper).collect(Collectors.toList());
31
+    }
32
+
33
+    public static <T> List<T> filter(final List<T> list,
34
+            Predicate<T> predicate) {
35
+        return list.stream().filter(predicate).collect(Collectors.toList());
36
+    }
37
+
38
+    public static <U, T> U reduce(final List<T> list,
39
+            U identity,
40
+            BiFunction<U, T, U> accumulator,
41
+            BinaryOperator<U> combiner) {
42
+        return list.stream().reduce(identity, accumulator, combiner);
43
+    }
44
+
45
+    public static <T> List<T> concat(final List<T>... lists) {
46
+        return Stream.of(lists)
47
+                .flatMap(Collection::stream)
48
+                .collect(Collectors.toList());
49
+    }
50
+
51
+}

+ 0
- 0
exercises/list-ops/src/main/java/.keep Просмотреть файл


+ 316
- 0
exercises/list-ops/src/test/java/ListOpsTest.java Просмотреть файл

1
+
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+import java.util.Collections;
5
+import java.util.List;
6
+import java.util.function.BiFunction;
7
+import java.util.function.BinaryOperator;
8
+import java.util.function.Predicate;
9
+import java.util.stream.Collectors;
10
+import java.util.stream.IntStream;
11
+import static junit.framework.TestCase.assertEquals;
12
+import static junit.framework.TestCase.assertFalse;
13
+import static junit.framework.TestCase.assertNotNull;
14
+import static junit.framework.TestCase.assertTrue;
15
+import org.junit.Ignore;
16
+import org.junit.Test;
17
+
18
+public class ListOpsTest {
19
+
20
+    private static final List<Integer> EMPTY_LIST
21
+            = Collections.emptyList();
22
+
23
+    @Test
24
+    public void lengthOfAnEmptyListShouldBeZero() {
25
+        final int expected = 0;
26
+        final int actual = ListOps.length(EMPTY_LIST);
27
+
28
+        assertEquals(expected, actual);
29
+    }
30
+
31
+    @Test
32
+    @Ignore
33
+    public void shouldReturnTheCorrectLengthOfAnNonEmptyList() {
34
+        final List<Integer> list = Collections.unmodifiableList(
35
+                Arrays.asList(0, 1, 2, 3, 4)
36
+        );
37
+        final int actual = ListOps.length(list);
38
+        final int expected = list.size();
39
+
40
+        assertEquals(expected, actual);
41
+    }
42
+
43
+    @Test
44
+    @Ignore
45
+    public void shouldReverseAnEmptyList() {
46
+        final List<Integer> actual = ListOps.reverse(EMPTY_LIST);
47
+
48
+        assertNotNull(actual);
49
+        assertTrue(actual.isEmpty());
50
+    }
51
+
52
+    @Test
53
+    @Ignore
54
+    public void shouldReverseANonEmptyList() {
55
+        final List<Integer> list = Collections.unmodifiableList(
56
+                Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8)
57
+        );
58
+        final List<Integer> actual
59
+                = ListOps.reverse(list);
60
+        final List<Integer> expected
61
+                = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1, 0);
62
+
63
+        assertNotNull(actual);
64
+        assertFalse(actual.isEmpty());
65
+        assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test
69
+    @Ignore
70
+    public void shouldMapAnEmptyListAndReturnAnEmptyList() {
71
+        final List<Integer> actual = ListOps.map(EMPTY_LIST, x -> x + 1);
72
+
73
+        assertNotNull(actual);
74
+        assertTrue(actual.isEmpty());
75
+    }
76
+
77
+    @Test
78
+    @Ignore
79
+    public void shouldMapNonEmptyList() {
80
+        final List<Integer> list
81
+                = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7));
82
+        final List<Integer> actual = ListOps.map(list, x -> x + 1);
83
+
84
+        assertNotNull(actual);
85
+        assertFalse(actual.isEmpty());
86
+        assertEquals(Arrays.asList(2, 4, 6, 8), actual);
87
+    }
88
+
89
+    @Test
90
+    @Ignore
91
+    public void shouldFilterAnEmptyListanddReturnAnEmptyList() {
92
+        final List<Integer> actual = ListOps.filter(EMPTY_LIST, x -> x > 0);
93
+
94
+        assertNotNull(actual);
95
+        assertTrue(actual.isEmpty());
96
+    }
97
+
98
+    @Test
99
+    @Ignore
100
+    public void shouldFilterNonEmptyList() {
101
+        Predicate<Integer> predicate = x -> x % 2 > 0;
102
+        final List<Integer> list = Collections.unmodifiableList(
103
+                IntStream.range(0, 100).boxed().collect(Collectors.toList())
104
+        );
105
+        final List<Integer> actual = ListOps.filter(list, predicate);
106
+        final List<Integer> expected = list.stream()
107
+                .filter(predicate)
108
+                .collect(Collectors.toList());
109
+
110
+        assertNotNull(actual);
111
+        assertFalse(actual.isEmpty());
112
+        assertEquals(expected, actual);
113
+    }
114
+
115
+    @Test
116
+    @Ignore
117
+    public void shouldConcatenateZeroLists() {
118
+        List<Integer> actual = ListOps.concat();
119
+
120
+        assertNotNull(actual);
121
+        assertTrue(actual.isEmpty());
122
+    }
123
+
124
+    @Test
125
+    @Ignore
126
+    public void shouldConcatenateOneNonEmptyList() {
127
+        final List<Integer> list
128
+                = Collections.unmodifiableList(
129
+                        Arrays.asList(0, 1, 2, 3, 4)
130
+                );
131
+        final List<Integer> actual = ListOps.concat(list);
132
+        final List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
133
+
134
+        assertNotNull(actual);
135
+        assertFalse(actual.isEmpty());
136
+        assertEquals(expected, actual);
137
+    }
138
+
139
+    @Test
140
+    @Ignore
141
+    public void shouldConcatenateOneEmptyList() {
142
+        final List<Integer> actual = ListOps.concat(EMPTY_LIST);
143
+
144
+        assertNotNull(actual);
145
+        assertTrue(actual.isEmpty());
146
+    }
147
+
148
+    @Test
149
+    @Ignore
150
+    public void shouldConcatenateTwoEmptyLists() {
151
+        final List<Integer> actual = ListOps.concat(EMPTY_LIST, EMPTY_LIST);
152
+
153
+        assertNotNull(actual);
154
+        assertTrue(actual.isEmpty());
155
+    }
156
+
157
+    @Test
158
+    @Ignore
159
+    public void shouldConcatenateOneEmptyAndOneNonEmptyLists() {
160
+        final List<Integer> list
161
+                = Collections.unmodifiableList(
162
+                        Arrays.asList(0, 1, 2, 3, 4)
163
+                );
164
+        final List<Integer> actual = ListOps.concat(list, EMPTY_LIST);
165
+        final List<Integer> expected
166
+                = Arrays.asList(0, 1, 2, 3, 4);
167
+
168
+        assertNotNull(actual);
169
+        assertFalse(actual.isEmpty());
170
+        assertEquals(expected, actual);
171
+    }
172
+
173
+    @Test
174
+    @Ignore
175
+    public void shouldConcatenateOneNonEmptyAndOneEmptyLists() {
176
+        final List<Integer> list
177
+                = Collections.unmodifiableList(
178
+                        Arrays.asList(0, 1, 2, 3, 4)
179
+                );
180
+        final List<Integer> actual = ListOps.concat(EMPTY_LIST, list);
181
+        final List<Integer> expected
182
+                = Arrays.asList(0, 1, 2, 3, 4);
183
+
184
+        assertNotNull(actual);
185
+        assertFalse(actual.isEmpty());
186
+        assertEquals(expected, actual);
187
+    }
188
+
189
+    @Test
190
+    @Ignore
191
+    public void shouldConcatenateTwoListsWithSameElements() {
192
+        final List<Integer> list1 = Collections.unmodifiableList(
193
+                Arrays.asList(0, 1, 2, 3, 4)
194
+        );
195
+        final List<Integer> list2 = Collections.unmodifiableList(
196
+                Arrays.asList(1, 2, 3, 4, 5, 6)
197
+        );
198
+        final List<Integer> expected
199
+                = Arrays.asList(0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6);
200
+        final List<Integer> actual = ListOps.concat(list1, list2);
201
+
202
+        assertNotNull(actual);
203
+        assertFalse(actual.isEmpty());
204
+        assertEquals(expected, actual);
205
+    }
206
+
207
+    @Test
208
+    @Ignore
209
+    public void shouldConcatenateSeveralLists() {
210
+        final List<Integer> list1 = Collections.unmodifiableList(
211
+                Arrays.asList(0, 1, 2, 3)
212
+        );
213
+        final List<Integer> list2 = Collections.unmodifiableList(
214
+                Arrays.asList(4, 5, 6, 7)
215
+        );
216
+        final List<Integer> list3 = Collections.unmodifiableList(
217
+                Arrays.asList(8, 9, 10, 11)
218
+        );
219
+        final List<Integer> list4 = Collections.unmodifiableList(
220
+                Arrays.asList(12, 13, 14, 15)
221
+        );
222
+        final List<Integer> expected
223
+                = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
224
+                        14, 15);
225
+
226
+        final List<Integer> actual
227
+                = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4);
228
+
229
+        assertNotNull(actual);
230
+        assertFalse(actual.isEmpty());
231
+        assertEquals(expected, actual);
232
+    }
233
+
234
+    @Test
235
+    @Ignore
236
+    public void shouldReturnIdentityWhenAnEmptyListIsReduced() {
237
+        final int expected = 0;
238
+        final int actual
239
+                = ListOps.reduce(EMPTY_LIST, 0, (x, y) -> x + y, Integer::sum);
240
+
241
+        assertEquals(expected, actual);
242
+    }
243
+
244
+    @Test
245
+    @Ignore
246
+    public void shouldCalculateTheSumOfANonEmptyIntegerList() {
247
+        final List<Integer> list = Collections.unmodifiableList(
248
+                Arrays.asList(0, 1, 2, 3, 4)
249
+        );
250
+        final int actual = ListOps.reduce(list, 0,
251
+                (x, y) -> x + y,
252
+                Integer::sum);
253
+
254
+        assertEquals(10, actual);
255
+    }
256
+
257
+    /*
258
+    https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
259
+    https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-U-java.util.function.BiFunction-java.util.function.BinaryOperator-
260
+     */
261
+    private BiFunction<List<Integer>, Integer, List<Integer>> accumulator
262
+            = (List<Integer> partial, Integer elem) -> {
263
+                List<Integer> result = new ArrayList<>(partial);
264
+                result.add(elem);
265
+                return result;
266
+            };
267
+
268
+    private BinaryOperator<List<Integer>> combiner
269
+            = (list1, list2) -> {
270
+                List<Integer> result = new ArrayList<>(list1);
271
+                result.addAll(list2);
272
+                return result;
273
+            };
274
+
275
+    @Test
276
+    @Ignore
277
+    public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() {
278
+        final List<Integer> list = Collections.unmodifiableList(
279
+                Arrays.asList(0, 1, 2, 3, 4, 5)
280
+        );
281
+        final List<Integer> actual
282
+                = ListOps.reduce(list,
283
+                        new ArrayList<Integer>(),
284
+                        accumulator,
285
+                        combiner);
286
+        final List<Integer> expected
287
+                = Arrays.asList(0, 1, 2, 3, 4, 5);
288
+
289
+        assertNotNull(actual);
290
+        assertFalse(actual.isEmpty());
291
+        assertEquals(expected, actual);
292
+    }
293
+
294
+    @Test
295
+    @Ignore
296
+    public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() {
297
+        final List<Integer> listOne = Collections.unmodifiableList(
298
+                Arrays.asList(0, 1, 2, 3, 4)
299
+        );
300
+        final List<Integer> listTwo = Collections.unmodifiableList(
301
+                Arrays.asList(5, 6, 7, 8, 9)
302
+        );
303
+        final List<Integer> actual
304
+                = ListOps.reduce(listTwo,
305
+                        listOne,
306
+                        accumulator,
307
+                        combiner);
308
+        final List<Integer> expected
309
+                = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
310
+
311
+        assertNotNull(actual);
312
+        assertFalse(actual.isEmpty());
313
+        assertEquals(expected, actual);
314
+    }
315
+
316
+}

+ 1
- 0
exercises/settings.gradle Просмотреть файл

25
 include 'hello-world'
25
 include 'hello-world'
26
 include 'largest-series-product'
26
 include 'largest-series-product'
27
 include 'linked-list'
27
 include 'linked-list'
28
+include 'list-ops'
28
 include 'luhn'
29
 include 'luhn'
29
 include 'matrix'
30
 include 'matrix'
30
 include 'meetup'
31
 include 'meetup'