Explorar el Código

custom-set: add to track (#206)

* dibs: I will implement custom-set

* Created an empty project

* Added custom-set exercise to the Java track
Dmitry Noranovich hace 9 años
padre
commit
6d8ec4feb9

+ 6
- 0
config.json Ver fichero

@@ -55,6 +55,7 @@
55 55
     "binary-search-tree",
56 56
     "binary-search",
57 57
     "all-your-base",
58
+    "custom-set",
58 59
     "wordy",
59 60
     "palindrome-products"
60 61
   ],
@@ -315,6 +316,11 @@
315 316
       "topics": []
316 317
     },
317 318
     {
319
+      "slug": "custom-set",
320
+      "difficulty": 1,
321
+      "topics": []
322
+    },
323
+    {
318 324
       "slug": "wordy",
319 325
       "difficulty": 1,
320 326
       "topics": []

+ 18
- 0
exercises/custom-set/build.gradle Ver fichero

@@ -0,0 +1,18 @@
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
+}

+ 74
- 0
exercises/custom-set/src/example/java/CustomSet.java Ver fichero

@@ -0,0 +1,74 @@
1
+
2
+import java.util.Collection;
3
+import java.util.Collections;
4
+import java.util.HashSet;
5
+import java.util.Set;
6
+import java.util.function.Predicate;
7
+import java.util.stream.Collectors;
8
+
9
+public class CustomSet<T> {
10
+
11
+    private Set<T> set;
12
+
13
+    public CustomSet() {
14
+        this(Collections.EMPTY_LIST);
15
+    }
16
+
17
+    public CustomSet(Collection<T> data) {
18
+        set = new HashSet<>(data.size());
19
+        this.set.addAll(data);
20
+    }
21
+
22
+    public boolean isEmpty() {
23
+        return set.isEmpty();
24
+    }
25
+
26
+    public boolean contains(T element) {
27
+        return set.contains(element);
28
+    }
29
+
30
+    public boolean isSubset(CustomSet<T> anotherSet) {
31
+        return set.containsAll(anotherSet.set);
32
+    }
33
+
34
+    public boolean isDisjoint(CustomSet<T> anotherSet) {
35
+        if (set.isEmpty() || anotherSet.set.isEmpty()) {
36
+            return true;
37
+        }
38
+        return set.stream()
39
+                .filter(elem -> anotherSet.set.contains(elem))
40
+                .count() == 0;
41
+    }
42
+
43
+    public boolean equals(CustomSet<T> anotherSet) {
44
+        return set.equals(anotherSet.set);
45
+    }
46
+
47
+    public boolean add(T element) {
48
+        return set.add(element);
49
+    }
50
+
51
+    public CustomSet<T> getIntersection(CustomSet<T> anotherSet) {
52
+        return new CustomSet<>(
53
+                set.stream()
54
+                        .filter(anotherSet.set::contains)
55
+                        .collect(Collectors.toList())
56
+        );
57
+    }
58
+
59
+    public CustomSet<T> getUnion(CustomSet<T> anotherSet) {
60
+        final Set<T> union = new HashSet<>(set);
61
+        union.addAll(anotherSet.set);
62
+        return new CustomSet<>(union);
63
+    }
64
+
65
+    public CustomSet<T> getDifference(CustomSet<T> anotherSet) {
66
+        final Predicate<T> predicate = anotherSet::contains;
67
+        return new CustomSet<>(
68
+                set.stream()
69
+                        .filter(predicate.negate())
70
+                        .collect(Collectors.toList())
71
+        );
72
+    }
73
+
74
+}

+ 0
- 0
exercises/custom-set/src/main/java/.keep Ver fichero


+ 505
- 0
exercises/custom-set/src/test/java/CustomSetTest.java Ver fichero

@@ -0,0 +1,505 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import static org.junit.Assert.assertFalse;
5
+import static org.junit.Assert.assertNotNull;
6
+import static org.junit.Assert.assertTrue;
7
+import org.junit.Test;
8
+import org.junit.Ignore;
9
+
10
+public class CustomSetTest {
11
+
12
+    @Test
13
+    public void setsWithNoElementsAreEmpty() {
14
+        final boolean actual
15
+                = new CustomSet<>(Collections.EMPTY_LIST)
16
+                        .isEmpty();
17
+        assertTrue(actual);
18
+    }
19
+
20
+    @Test
21
+    @Ignore
22
+    public void setsWithElementsAreNotEmpty() {
23
+        final boolean actual
24
+                = new CustomSet<>(Arrays.asList(1))
25
+                        .isEmpty();
26
+
27
+        assertFalse(actual);
28
+    }
29
+
30
+    @Test
31
+    @Ignore
32
+    public void nothingIsContainedInAnEmptySet() {
33
+        final boolean actual
34
+                = new CustomSet<>(Collections.EMPTY_LIST)
35
+                        .contains(1);
36
+
37
+        assertFalse(actual);
38
+    }
39
+
40
+    @Test
41
+    @Ignore
42
+    public void whenTheElementIsInTheSet() {
43
+        final boolean actual
44
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
45
+                        .contains(1);
46
+
47
+        assertTrue(actual);
48
+    }
49
+
50
+    @Test
51
+    @Ignore
52
+    public void whenTheElementIsNotInTheSet() {
53
+        final boolean actual
54
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
55
+                        .contains(4);
56
+
57
+        assertFalse(actual);
58
+    }
59
+
60
+    @Test
61
+    @Ignore
62
+    public void emptySetIsASubsetOfAnotherEmptySet() {
63
+        final boolean actual
64
+                = new CustomSet<>(Collections.EMPTY_LIST)
65
+                        .isSubset(
66
+                                new CustomSet<>(Collections.EMPTY_LIST)
67
+                        );
68
+
69
+        assertTrue(actual);
70
+    }
71
+
72
+    @Test
73
+    @Ignore
74
+    public void emptySetIsASubsetOfNonEemptySet() {
75
+        final boolean actual
76
+                = new CustomSet<>(Arrays.asList(1))
77
+                        .isSubset(
78
+                                new CustomSet<>(Collections.EMPTY_LIST)
79
+                        );
80
+
81
+        assertTrue(actual);
82
+    }
83
+
84
+    @Test
85
+    @Ignore
86
+    public void nonEmptySetIsNotASubsetOfEmptySet() {
87
+        final boolean actual
88
+                = new CustomSet<>(Collections.EMPTY_LIST)
89
+                        .isSubset(
90
+                                new CustomSet<>(Arrays.asList(1))
91
+                        );
92
+
93
+        assertFalse(actual);
94
+    }
95
+
96
+    @Test
97
+    @Ignore
98
+    public void setIsASubsetOfSetWithExactSameElements() {
99
+        final boolean actual
100
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
101
+                        .isSubset(
102
+                                new CustomSet<>(Arrays.asList(1, 2, 3))
103
+                        );
104
+
105
+        assertTrue(actual);
106
+    }
107
+
108
+    @Test
109
+    @Ignore
110
+    public void setIsASubsetOfLargerSetWithSameElements() {
111
+        final boolean actual
112
+                = new CustomSet<>(Arrays.asList(4, 1, 2, 3))
113
+                        .isSubset(
114
+                                new CustomSet<>(Arrays.asList(1, 2, 3))
115
+                        );
116
+
117
+        assertTrue(actual);
118
+    }
119
+
120
+    @Test
121
+    @Ignore
122
+    public void setIsNotASubsetOfSetThatDoesNotContainItsElements() {
123
+        final boolean actual
124
+                = new CustomSet<>(Arrays.asList(4, 1, 3))
125
+                        .isSubset(
126
+                                new CustomSet<>(Arrays.asList(1, 2, 3))
127
+                        );
128
+
129
+        assertFalse(actual);
130
+    }
131
+
132
+    @Test
133
+    @Ignore
134
+    public void theEmptySetIsDisjointWithItself() {
135
+        final boolean actual
136
+                = new CustomSet<>(Collections.EMPTY_LIST)
137
+                        .isDisjoint(
138
+                                new CustomSet<>(Collections.EMPTY_LIST)
139
+                        );
140
+
141
+        assertTrue(actual);
142
+    }
143
+
144
+    @Test
145
+    @Ignore
146
+    public void emptySetIsDisjointWithNonEmptySet() {
147
+        final boolean actual
148
+                = new CustomSet<>(Collections.EMPTY_LIST)
149
+                        .isDisjoint(
150
+                                new CustomSet<>(Arrays.asList(1))
151
+                        );
152
+
153
+        assertTrue(actual);
154
+    }
155
+
156
+    @Test
157
+    @Ignore
158
+    public void nonEmptySetIsDisjointWithEmptySet() {
159
+        final boolean actual
160
+                = new CustomSet<>(Arrays.asList(1))
161
+                        .isDisjoint(
162
+                                new CustomSet<>(Collections.EMPTY_LIST)
163
+                        );
164
+
165
+        assertTrue(actual);
166
+    }
167
+
168
+    @Test
169
+    @Ignore
170
+    public void setsAreNotDisjointIfTheyShareAnElement() {
171
+        final boolean actual
172
+                = new CustomSet<>(Arrays.asList(1, 2))
173
+                        .isDisjoint(
174
+                                new CustomSet<>(Arrays.asList(2, 3))
175
+                        );
176
+
177
+        assertFalse(actual);
178
+    }
179
+
180
+    @Test
181
+    @Ignore
182
+    public void setsAreDisjointIfTheyShareNoElements() {
183
+        final boolean actual
184
+                = new CustomSet<>(Arrays.asList(1, 2))
185
+                        .isDisjoint(
186
+                                new CustomSet<>(Arrays.asList(3, 4))
187
+                        );
188
+
189
+        assertTrue(actual);
190
+    }
191
+
192
+    @Test
193
+    @Ignore
194
+    public void emptySetsAreEqual() {
195
+        final boolean actual
196
+                = new CustomSet<>(Collections.EMPTY_LIST)
197
+                        .equals(
198
+                                new CustomSet<>(Collections.EMPTY_LIST)
199
+                        );
200
+
201
+        assertTrue(actual);
202
+    }
203
+
204
+    @Test
205
+    @Ignore
206
+    public void emptySetIsNotEqualToNonEmptySet() {
207
+        final boolean actual
208
+                = new CustomSet<>(Collections.EMPTY_LIST)
209
+                        .equals(
210
+                                new CustomSet<>(Arrays.asList(1, 2, 3))
211
+                        );
212
+
213
+        assertFalse(actual);
214
+    }
215
+
216
+    @Test
217
+    @Ignore
218
+    public void nonEmptySetIsNotEqualToEmptySet() {
219
+        final boolean actual
220
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
221
+                        .equals(
222
+                                new CustomSet<>(Collections.EMPTY_LIST)
223
+                        );
224
+
225
+        assertFalse(actual);
226
+    }
227
+
228
+    @Test
229
+    @Ignore
230
+    public void setsWithTheSameElementsAreEqual() {
231
+        final boolean actual
232
+                = new CustomSet<>(Arrays.asList(1, 2))
233
+                        .equals(
234
+                                new CustomSet<>(Arrays.asList(2, 1))
235
+                        );
236
+
237
+        assertTrue(actual);
238
+    }
239
+
240
+    @Test
241
+    @Ignore
242
+    public void setsWithDifferentElementsAreNotEqual() {
243
+        final boolean actual
244
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
245
+                        .equals(
246
+                                new CustomSet<>(Arrays.asList(1, 2, 4))
247
+                        );
248
+
249
+        assertFalse(actual);
250
+    }
251
+
252
+    @Test
253
+    @Ignore
254
+    public void addToEmptySet() {
255
+        final int element = 3;
256
+        final CustomSet<Integer> expected
257
+                = new CustomSet<>(
258
+                        Collections.unmodifiableList(Arrays.asList(element))
259
+                );
260
+        final CustomSet<Integer> actual
261
+                = new CustomSet<>(Collections.EMPTY_LIST);
262
+
263
+        actual.add(element);
264
+
265
+        assertNotNull(actual);
266
+        assertFalse(actual.isEmpty());
267
+        assertTrue(expected.equals(actual));
268
+    }
269
+
270
+    @Test
271
+    @Ignore
272
+    public void addToNonEmptySet() {
273
+        final int element = 3;
274
+        final CustomSet<Integer> expected
275
+                = new CustomSet<>(
276
+                        Collections.unmodifiableList(Arrays.asList(1, 2, 3, 4))
277
+                );
278
+        final CustomSet<Integer> actual
279
+                = new CustomSet<>(Arrays.asList(1, 2, 4));
280
+
281
+        actual.add(element);
282
+
283
+        assertNotNull(actual);
284
+        assertFalse(actual.isEmpty());
285
+        assertTrue(expected.equals(actual));
286
+    }
287
+
288
+    @Test
289
+    @Ignore
290
+    public void addingAnExistingElementDoesNotChangeTheSet() {
291
+        final int element = 3;
292
+        final CustomSet<Integer> expected
293
+                = new CustomSet<>(
294
+                        Collections.unmodifiableList(Arrays.asList(1, 2, 3))
295
+                );
296
+        final CustomSet<Integer> actual
297
+                = new CustomSet<>(Arrays.asList(1, 2, 3));
298
+
299
+        actual.add(element);
300
+
301
+        assertNotNull(actual);
302
+        assertTrue(expected.equals(actual));
303
+    }
304
+
305
+    @Test
306
+    @Ignore
307
+    public void intersectionOfTwoEmptySetsIsAnEmptySet() {
308
+        final CustomSet<Integer> actual
309
+                = new CustomSet<>(Collections.EMPTY_LIST)
310
+                        .getIntersection(
311
+                                new CustomSet<>(Collections.EMPTY_LIST)
312
+                        );
313
+
314
+        assertNotNull(actual);
315
+        assertTrue(actual.isEmpty());
316
+    }
317
+
318
+    @Test
319
+    @Ignore
320
+    public void intersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
321
+        final CustomSet<Integer> actual
322
+                = new CustomSet<>(Collections.EMPTY_LIST)
323
+                        .getIntersection(
324
+                                new CustomSet<>(Arrays.asList(3, 2, 5))
325
+                        );
326
+
327
+        assertNotNull(actual);
328
+        assertTrue(actual.isEmpty());
329
+    }
330
+
331
+    @Test
332
+    @Ignore
333
+    public void intersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() {
334
+        final CustomSet<Integer> actual
335
+                = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
336
+                        .getIntersection(
337
+                                new CustomSet<>(Collections.EMPTY_LIST)
338
+                        );
339
+
340
+        assertNotNull(actual);
341
+        assertTrue(actual.isEmpty());
342
+
343
+    }
344
+
345
+    @Test
346
+    @Ignore
347
+    public void intersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() {
348
+        final CustomSet<Integer> actual
349
+                = new CustomSet<>(Arrays.asList(1, 2, 3))
350
+                        .getIntersection(
351
+                                new CustomSet<>(Arrays.asList(4, 5, 6))
352
+                        );
353
+
354
+        assertNotNull(actual);
355
+        assertTrue(actual.isEmpty());
356
+    }
357
+
358
+    @Test
359
+    @Ignore
360
+    public void intersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() {
361
+        final CustomSet<Integer> expected
362
+                = new CustomSet<>(
363
+                        Collections.unmodifiableList(Arrays.asList(2, 3))
364
+                );
365
+        final CustomSet<Integer> actual
366
+                = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
367
+                        .getIntersection(
368
+                                new CustomSet<>(Arrays.asList(3, 2, 5))
369
+                        );
370
+
371
+        assertNotNull(actual);
372
+        assertFalse(actual.isEmpty());
373
+        assertTrue(expected.equals(actual));
374
+    }
375
+
376
+    @Test
377
+    @Ignore
378
+    public void differenceOfTwoEmptySetsIsAnEmptySet() {
379
+        final CustomSet<Integer> actual
380
+                = new CustomSet<>(Collections.EMPTY_LIST)
381
+                        .getDifference(
382
+                                new CustomSet<>(Collections.EMPTY_LIST)
383
+                        );
384
+
385
+        assertNotNull(actual);
386
+        assertTrue(actual.isEmpty());
387
+    }
388
+
389
+    @Test
390
+    @Ignore
391
+    public void differenceOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
392
+        final CustomSet<Integer> actual
393
+                = new CustomSet<>(Collections.EMPTY_LIST)
394
+                        .getDifference(
395
+                                new CustomSet<>(Arrays.asList(3, 2, 5))
396
+                        );
397
+
398
+        assertNotNull(actual);
399
+        assertTrue(actual.isEmpty());
400
+    }
401
+
402
+    @Test
403
+    @Ignore
404
+    public void differenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
405
+        final CustomSet<Integer> expected
406
+                = new CustomSet<>(
407
+                        Collections.unmodifiableList(Arrays.asList(1, 2, 3, 4))
408
+                );
409
+        final CustomSet<Integer> actual
410
+                = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
411
+                        .getDifference(
412
+                                new CustomSet<>(Collections.EMPTY_LIST)
413
+                        );
414
+
415
+        assertNotNull(actual);
416
+        assertFalse(actual.isEmpty());
417
+        assertTrue(expected.equals(actual));
418
+    }
419
+
420
+    @Test
421
+    @Ignore
422
+    public void differenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() {
423
+        final CustomSet<Integer> expected
424
+                = new CustomSet<>(
425
+                        Collections.unmodifiableList(Arrays.asList(1, 3))
426
+                );
427
+        final CustomSet<Integer> actual
428
+                = new CustomSet<>(Arrays.asList(3, 2, 1))
429
+                        .getDifference(
430
+                                new CustomSet<>(Arrays.asList(2, 4))
431
+                        );
432
+
433
+        assertNotNull(actual);
434
+        assertFalse(actual.isEmpty());
435
+        assertTrue(expected.equals(actual));
436
+    }
437
+
438
+    @Test
439
+    @Ignore
440
+    public void unionOfTwoEmptySetsIsAnEmptySet() {
441
+        final CustomSet<Integer> actual
442
+                = new CustomSet<>(Collections.EMPTY_LIST)
443
+                        .getUnion(
444
+                                new CustomSet<>(Collections.EMPTY_LIST)
445
+                        );
446
+
447
+        assertNotNull(actual);
448
+        assertTrue(actual.isEmpty());
449
+    }
450
+
451
+    @Test
452
+    @Ignore
453
+    public void unionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() {
454
+        final CustomSet<Integer> expected
455
+                = new CustomSet<>(
456
+                        Collections.unmodifiableList(Arrays.asList(2))
457
+                );
458
+        final CustomSet<Integer> actual
459
+                = new CustomSet<>(Collections.EMPTY_LIST)
460
+                        .getUnion(
461
+                                new CustomSet<>(Arrays.asList(2))
462
+                        );
463
+
464
+        assertNotNull(actual);
465
+        assertFalse(actual.isEmpty());
466
+        assertTrue(expected.equals(actual));
467
+    }
468
+
469
+    @Test
470
+    @Ignore
471
+    public void unionOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
472
+        final CustomSet<Integer> expected
473
+                = new CustomSet<>(
474
+                        Collections.unmodifiableList(Arrays.asList(1, 3))
475
+                );
476
+        final CustomSet<Integer> actual
477
+                = new CustomSet<>(Arrays.asList(1, 3))
478
+                        .getUnion(
479
+                                new CustomSet<>(Collections.EMPTY_LIST)
480
+                        );
481
+
482
+        assertNotNull(actual);
483
+        assertFalse(actual.isEmpty());
484
+        assertTrue(expected.equals(actual));
485
+    }
486
+
487
+    @Test
488
+    @Ignore
489
+    public void unionOfTwoNonEmptySetsContainsAllUniqueElements() {
490
+        final CustomSet<Integer> expected
491
+                = new CustomSet<>(
492
+                        Collections.unmodifiableList(Arrays.asList(3, 2, 1))
493
+                );
494
+        final CustomSet<Integer> actual
495
+                = new CustomSet<>(Arrays.asList(1, 3))
496
+                        .getUnion(
497
+                                new CustomSet<>(Arrays.asList(2, 3))
498
+                        );
499
+
500
+        assertNotNull(actual);
501
+        assertFalse(actual.isEmpty());
502
+        assertTrue(expected.equals(actual));
503
+    }
504
+
505
+}

+ 1
- 0
exercises/settings.gradle Ver fichero

@@ -10,6 +10,7 @@ include 'binary-search-tree'
10 10
 include 'bob'
11 11
 include 'bracket-push'
12 12
 include 'crypto-square'
13
+include 'custom-set'
13 14
 include 'difference-of-squares'
14 15
 include 'etl'
15 16
 include 'gigasecond'