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

Merge pull request #182 from javaeeeee/javaeeeee

Javaeeeee
John Ryan 9 лет назад
Родитель
Сommit
583565b40e

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

@@ -46,7 +46,8 @@
46 46
     "difference-of-squares",
47 47
     "largest-series-product",
48 48
     "queen-attack",
49
-    "minesweeper"
49
+    "minesweeper",
50
+    "series"
50 51
   ],
51 52
   "exercises": [
52 53
     {
@@ -263,6 +264,11 @@
263 264
       "slug": "minesweeper",
264 265
       "difficulty": 1,
265 266
       "topics": []
267
+    },
268
+    {
269
+      "slug": "series",
270
+      "difficulty": 1,
271
+      "topics": []
266 272
     }
267 273
   ],
268 274
   "deprecated": [

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

@@ -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
+  testCompile "org.assertj:assertj-core:3.2.0"
12
+}
13
+test {
14
+  testLogging {
15
+    exceptionFormat = 'full'
16
+    events = ["passed", "failed", "skipped"]
17
+  }
18
+}

+ 39
- 0
exercises/series/src/example/java/Series.java Просмотреть файл

@@ -0,0 +1,39 @@
1
+
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+import java.util.List;
5
+import java.util.stream.Collectors;
6
+
7
+public class Series {
8
+
9
+    private final int digitsSize;
10
+    private List<Integer> digits;
11
+
12
+    public Series(String string) {
13
+        this.digits = Arrays
14
+                .asList(string.split(("")))
15
+                .stream()
16
+                .map(digit -> Integer.parseInt(digit))
17
+                .collect(Collectors.toList());
18
+        this.digitsSize = this.digits.size();
19
+    }
20
+
21
+    public List<List<Integer>> slices(int num) {
22
+        if (num > this.digitsSize) {
23
+            throw new IllegalArgumentException("Slice size is too big.");
24
+        }
25
+        final int limit = this.digitsSize - num + 1;
26
+        List<List<Integer>> result = new ArrayList<>(limit);
27
+        List<Integer> tmp;
28
+        for (int i = 0; i < limit; i++) {
29
+            tmp = this.digits.subList(i, i + num);
30
+            result.add(tmp);
31
+        }
32
+        return result;
33
+    }
34
+
35
+    public List<Integer> getDigits() {
36
+        return digits;
37
+    }
38
+
39
+}

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


+ 3
- 0
exercises/series/src/main/java/Series.java Просмотреть файл

@@ -0,0 +1,3 @@
1
+public class Series {
2
+    
3
+}

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


+ 154
- 0
exercises/series/src/test/java/SeriesTest.java Просмотреть файл

@@ -0,0 +1,154 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.List;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.assertEquals;
7
+import static org.junit.Assert.assertFalse;
8
+import static org.junit.Assert.assertNotNull;
9
+import org.junit.Ignore;
10
+
11
+public class SeriesTest {
12
+
13
+    @Test
14
+    public void hasDigitsShort() {
15
+        Series sut = new Series("01234");
16
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
17
+        List<Integer> actual = sut.getDigits();
18
+        assertNotNull(actual);
19
+        assertFalse(actual.isEmpty());
20
+        assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    @Ignore
25
+    public void hasDigitsLong() {
26
+        Series sut = new Series("0123456789");
27
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
28
+        List<Integer> actual = sut.getDigits();
29
+        assertNotNull(actual);
30
+        assertFalse(actual.isEmpty());
31
+        assertEquals(expected, actual);
32
+    }
33
+
34
+    @Test
35
+    @Ignore
36
+    public void keepsTheDigitOrderIfReversed() {
37
+        Series sut = new Series("9876543210");
38
+        List<Integer> expected = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
39
+        List<Integer> actual = sut.getDigits();
40
+        assertNotNull(actual);
41
+        assertFalse(actual.isEmpty());
42
+        assertEquals(expected, actual);
43
+    }
44
+
45
+    @Test
46
+    @Ignore
47
+    public void keepsArbitraryDigitOrder() {
48
+        Series sut = new Series("936923468");
49
+        List<Integer> expected = Arrays.asList(9, 3, 6, 9, 2, 3, 4, 6, 8);
50
+        List<Integer> actual = sut.getDigits();
51
+        assertNotNull(actual);
52
+        assertFalse(actual.isEmpty());
53
+        assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    @Ignore
58
+    public void canSliceByOne() {
59
+        Series sut = new Series("01234");
60
+        List<List<Integer>> expected = Arrays.asList(
61
+                Arrays.asList(0),
62
+                Arrays.asList(1),
63
+                Arrays.asList(2),
64
+                Arrays.asList(3),
65
+                Arrays.asList(4)
66
+        );
67
+        List<List<Integer>> actual = sut.slices(1);
68
+        assertNotNull(actual);
69
+        assertFalse(actual.isEmpty());
70
+        assertEquals(expected, actual);
71
+    }
72
+
73
+    @Test
74
+    @Ignore
75
+    public void canSliceByTwo() {
76
+        Series sut = new Series("98273463");
77
+        List<List<Integer>> expected = Arrays.asList(
78
+                Arrays.asList(9, 8),
79
+                Arrays.asList(8, 2),
80
+                Arrays.asList(2, 7),
81
+                Arrays.asList(7, 3),
82
+                Arrays.asList(3, 4),
83
+                Arrays.asList(4, 6),
84
+                Arrays.asList(6, 3)
85
+        );
86
+        List<List<Integer>> actual = sut.slices(2);
87
+        assertNotNull(actual);
88
+        assertFalse(actual.isEmpty());
89
+        assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    @Ignore
94
+    public void canSliceByThree() {
95
+        Series sut = new Series("01234");
96
+        List<List<Integer>> expected = Arrays.asList(
97
+                Arrays.asList(0, 1, 2),
98
+                Arrays.asList(1, 2, 3),
99
+                Arrays.asList(2, 3, 4)
100
+        );
101
+        List<List<Integer>> actual = sut.slices(3);
102
+        assertNotNull(actual);
103
+        assertFalse(actual.isEmpty());
104
+        assertEquals(expected, actual);
105
+    }
106
+
107
+    @Test
108
+    @Ignore
109
+    public void canSliceByThreeWithDuplicateDigits() {
110
+        Series sut = new Series("31001");
111
+        List<List<Integer>> expected = Arrays.asList(
112
+                Arrays.asList(3, 1, 0),
113
+                Arrays.asList(1, 0, 0),
114
+                Arrays.asList(0, 0, 1)
115
+        );
116
+        List<List<Integer>> actual = sut.slices(3);
117
+        assertNotNull(actual);
118
+        assertFalse(actual.isEmpty());
119
+        assertEquals(expected, actual);
120
+    }
121
+
122
+    @Test
123
+    @Ignore
124
+    public void canSliceByFour() {
125
+        Series sut = new Series("91274");
126
+        List<List<Integer>> expected = Arrays.asList(
127
+                Arrays.asList(9, 1, 2, 7),
128
+                Arrays.asList(1, 2, 7, 4)
129
+        );
130
+        List<List<Integer>> actual = sut.slices(4);
131
+        assertNotNull(actual);
132
+        assertFalse(actual.isEmpty());
133
+        assertEquals(expected, actual);
134
+    }
135
+
136
+    @Test
137
+    @Ignore
138
+    public void canSliceByFive() {
139
+        Series sut = new Series("81228");
140
+        List<List<Integer>> expected = Arrays.asList(
141
+                Arrays.asList(8, 1, 2, 2, 8)
142
+        );
143
+        List<List<Integer>> actual = sut.slices(5);
144
+        assertNotNull(actual);
145
+        assertFalse(actual.isEmpty());
146
+        assertEquals(expected, actual);
147
+    }
148
+
149
+    @Test(expected = IllegalArgumentException.class)
150
+    @Ignore
151
+    public void throwsAnErrorIfNotEnoughDigitsToSlice() {
152
+        new Series("01032987583").slices(12);
153
+    }
154
+}

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

@@ -33,6 +33,7 @@ include 'robot-name'
33 33
 include 'roman-numerals'
34 34
 include 'prime-factors'
35 35
 include 'scrabble-score'
36
+include 'series'
36 37
 include 'sieve'
37 38
 include 'simple-cipher'
38 39
 include 'simple-linked-list'