Преглед на файлове

ocr-numbers: add to track (#312)

Stuart Kent преди 9 години
родител
ревизия
19aa29e5f5

+ 5
- 0
config.json Целия файл

@@ -308,6 +308,11 @@
308 308
       "slug": "list-ops",
309 309
       "difficulty": 1,
310 310
       "topics": []
311
+    },
312
+    {
313
+      "slug": "ocr-numbers",
314
+      "difficulty": 1,
315
+      "topics": []
311 316
     }
312 317
   ],
313 318
   "deprecated": [

+ 17
- 0
exercises/ocr-numbers/build.gradle Целия файл

@@ -0,0 +1,17 @@
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
+test {
13
+  testLogging {
14
+    exceptionFormat = 'full'
15
+    events = ["passed", "failed", "skipped"]
16
+  }
17
+}

+ 63
- 0
exercises/ocr-numbers/src/example/java/Digit.java Целия файл

@@ -0,0 +1,63 @@
1
+import java.util.List;
2
+
3
+import static java.util.Arrays.asList;
4
+import static java.util.Arrays.stream;
5
+
6
+enum Digit {
7
+
8
+    ZERO(asList(" _ ",
9
+                "| |",
10
+                "|_|")),
11
+
12
+    ONE(asList("   ",
13
+               "  |",
14
+               "  |")),
15
+
16
+    TWO(asList(" _ ",
17
+               " _|",
18
+               "|_ ")),
19
+
20
+    THREE(asList(" _ ",
21
+                 " _|",
22
+                 " _|")),
23
+
24
+    FOUR(asList("   ",
25
+                "|_|",
26
+                "  |")),
27
+
28
+    FIVE(asList(" _ ",
29
+                "|_ ",
30
+                " _|")),
31
+
32
+    SIX(asList(" _ ",
33
+               "|_ ",
34
+               "|_|")),
35
+
36
+    SEVEN(asList(" _ ",
37
+                 "  |",
38
+                 "  |")),
39
+
40
+    EIGHT(asList(" _ ",
41
+                 "|_|",
42
+                 "|_|")),
43
+
44
+    NINE(asList(" _ ",
45
+                "|_|",
46
+                " _|"));
47
+
48
+    private final List<String> ssdRepresentation;
49
+
50
+    Digit(List<String> ssd) {
51
+        this.ssdRepresentation = ssd;
52
+    }
53
+
54
+    static String fromSsdConfiguration(final List<String> ssdConfiguration) {
55
+        return stream(values())
56
+                .filter(digit -> digit.ssdRepresentation.equals(ssdConfiguration))
57
+                .map(Digit::ordinal)
58
+                .map(Object::toString)
59
+                .findFirst()
60
+                .orElse("?");
61
+    }
62
+
63
+}

+ 67
- 0
exercises/ocr-numbers/src/example/java/OpticalCharacterReader.java Целия файл

@@ -0,0 +1,67 @@
1
+import java.util.ArrayList;
2
+import java.util.List;
3
+
4
+/*
5
+ * This example solution uses the abbreviation "SSD", short for Seven-Segment Display, throughout.
6
+ *
7
+ * For more information, see https://en.wikipedia.org/wiki/Seven-segment_display.
8
+ */
9
+final class OpticalCharacterReader {
10
+
11
+    private static final int ROWS_PER_LINE  = 4;
12
+
13
+    private static final int COLS_PER_SSD = 3;
14
+
15
+    String parse(final List<String> input) {
16
+        validateInput(input);
17
+
18
+        final List<String> parsedLines = new ArrayList<>();
19
+
20
+        for (int nLine = 0; nLine < input.size() / ROWS_PER_LINE; nLine++) {
21
+            final int nFirstRowCurrentLine = nLine * ROWS_PER_LINE;
22
+            final int nFirstRowNextLine = nFirstRowCurrentLine + ROWS_PER_LINE;
23
+
24
+            final List<String> currentLine = input.subList(nFirstRowCurrentLine, nFirstRowNextLine);
25
+            parsedLines.add(parseLine(currentLine));
26
+        }
27
+
28
+        return String.join(",", parsedLines);
29
+    }
30
+
31
+    private String parseLine(final List<String> currentLine) {
32
+        final List<String> parsedDigits = new ArrayList<>();
33
+
34
+        for (int nSsd = 0; nSsd < currentLine.get(0).length() / COLS_PER_SSD; nSsd++) {
35
+            final int nFirstColCurrentSsd = nSsd * COLS_PER_SSD;
36
+            final int nFirstColNextSsd = nFirstColCurrentSsd + COLS_PER_SSD;
37
+
38
+            final List<String> currentSsdConfiguration = new ArrayList<>();
39
+
40
+            // Bottom row of each line is a spacer, so we ignore that row when constructing SSD configurations.
41
+            for (int nRow = 0; nRow < ROWS_PER_LINE - 1; nRow++) {
42
+                currentSsdConfiguration.add(currentLine.get(nRow).substring(nFirstColCurrentSsd, nFirstColNextSsd));
43
+            }
44
+
45
+            parsedDigits.add(Digit.fromSsdConfiguration(currentSsdConfiguration));
46
+        }
47
+
48
+        return String.join("", parsedDigits);
49
+    }
50
+
51
+    private void validateInput(final List<String> input) {
52
+        final int inputRowCount = input.size();
53
+
54
+        if (inputRowCount == 0 || inputRowCount % 4 != 0) {
55
+            throw new IllegalArgumentException(
56
+                    "Number of input rows must be a positive multiple of " + ROWS_PER_LINE);
57
+        }
58
+
59
+        final int inputColumnCount = input.get(0).length();
60
+
61
+        if (inputColumnCount == 0 || inputColumnCount % 3 != 0) {
62
+            throw new IllegalArgumentException(
63
+                    "Number of input columns must be a positive multiple of " + COLS_PER_SSD);
64
+        }
65
+    }
66
+
67
+}

+ 5
- 0
exercises/ocr-numbers/src/main/java/OpticalCharacterReader.java Целия файл

@@ -0,0 +1,5 @@
1
+final class OpticalCharacterReader {
2
+
3
+
4
+
5
+}

+ 248
- 0
exercises/ocr-numbers/src/test/java/OpticalCharacterReaderTest.java Целия файл

@@ -0,0 +1,248 @@
1
+import org.junit.Ignore;
2
+import org.junit.Rule;
3
+import org.junit.Test;
4
+import org.junit.rules.ExpectedException;
5
+
6
+import java.util.Arrays;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+
10
+public final class OpticalCharacterReaderTest {
11
+
12
+    /*
13
+     * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
14
+     * ExpectedExceptions in particular.
15
+     */
16
+    @Rule
17
+    public ExpectedException expectedException = ExpectedException.none();
18
+
19
+    @Test
20
+    public void testReaderRecognizesSingle0() {
21
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
22
+                " _ ",
23
+                "| |",
24
+                "|_|",
25
+                "   "
26
+        ));
27
+
28
+        assertEquals("0", parsedInput);
29
+    }
30
+
31
+    @Ignore
32
+    @Test
33
+    public void testReaderRecognizesSingle1() {
34
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
35
+                "   ",
36
+                "  |",
37
+                "  |",
38
+                "   "
39
+        ));
40
+
41
+        assertEquals("1", parsedInput);
42
+    }
43
+
44
+    @Ignore
45
+    @Test
46
+    public void testReaderReturnsQuestionMarkForUnreadableButCorrectlySizedInput() {
47
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
48
+                "   ",
49
+                "  _",
50
+                "  |",
51
+                "   "
52
+        ));
53
+
54
+        assertEquals("?", parsedInput);
55
+    }
56
+
57
+    @Ignore
58
+    @Test
59
+    public void testReaderThrowsExceptionWhenNumberOfInputLinesIsNotAMultipleOf4() {
60
+        expectedException.expect(IllegalArgumentException.class);
61
+        expectedException.expectMessage("Number of input rows must be a positive multiple of 4");
62
+
63
+        new OpticalCharacterReader().parse(Arrays.asList(
64
+                " _ ",
65
+                "| |",
66
+                "   "
67
+        ));
68
+    }
69
+
70
+    @Ignore
71
+    @Test
72
+    public void testReaderThrowsExceptionWhenNumberOfInputColumnsIsNotAMultipleOf3() {
73
+        expectedException.expect(IllegalArgumentException.class);
74
+        expectedException.expectMessage("Number of input columns must be a positive multiple of 3");
75
+
76
+        new OpticalCharacterReader().parse(Arrays.asList(
77
+                "    ",
78
+                "   |",
79
+                "   |",
80
+                "    "
81
+        ));
82
+    }
83
+
84
+    @Ignore
85
+    @Test
86
+    public void testReaderRecognizesBinarySequence110101100() {
87
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
88
+                "       _     _        _  _ ",
89
+                "  |  || |  || |  |  || || |",
90
+                "  |  ||_|  ||_|  |  ||_||_|",
91
+                "                           "
92
+        ));
93
+
94
+        assertEquals("110101100", parsedInput);
95
+    }
96
+
97
+    @Ignore
98
+    @Test
99
+    public void testReaderReplacesUnreadableDigitsWithQuestionMarksWithinSequence() {
100
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
101
+                "       _     _           _ ",
102
+                "  |  || |  || |     || || |",
103
+                "  |  | _|  ||_|  |  ||_||_|",
104
+                "                           "
105
+        ));
106
+
107
+        assertEquals("11?10?1?0", parsedInput);
108
+    }
109
+
110
+    @Ignore
111
+    @Test
112
+    public void testReaderRecognizesSingle2() {
113
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
114
+                " _ ",
115
+                " _|",
116
+                "|_ ",
117
+                "   "
118
+        ));
119
+
120
+        assertEquals("2", parsedInput);
121
+    }
122
+
123
+    @Ignore
124
+    @Test
125
+    public void testReaderRecognizesSingle3() {
126
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
127
+                " _ ",
128
+                " _|",
129
+                " _|",
130
+                "   "
131
+        ));
132
+
133
+        assertEquals("3", parsedInput);
134
+    }
135
+
136
+    @Ignore
137
+    @Test
138
+    public void testReaderRecognizesSingle4() {
139
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
140
+                "   ",
141
+                "|_|",
142
+                "  |",
143
+                "   "
144
+        ));
145
+
146
+        assertEquals("4", parsedInput);
147
+    }
148
+
149
+    @Ignore
150
+    @Test
151
+    public void testReaderRecognizesSingle5() {
152
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
153
+                " _ ",
154
+                "|_ ",
155
+                " _|",
156
+                "   "
157
+        ));
158
+
159
+        assertEquals("5", parsedInput);
160
+    }
161
+
162
+    @Ignore
163
+    @Test
164
+    public void testReaderRecognizesSingle6() {
165
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
166
+                " _ ",
167
+                "|_ ",
168
+                "|_|",
169
+                "   "
170
+        ));
171
+
172
+        assertEquals("6", parsedInput);
173
+    }
174
+
175
+    @Ignore
176
+    @Test
177
+    public void testReaderRecognizesSingle7() {
178
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
179
+                " _ ",
180
+                "  |",
181
+                "  |",
182
+                "   "
183
+        ));
184
+
185
+        assertEquals("7", parsedInput);
186
+    }
187
+
188
+    @Ignore
189
+    @Test
190
+    public void testReaderRecognizesSingle8() {
191
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
192
+                " _ ",
193
+                "|_|",
194
+                "|_|",
195
+                "   "
196
+        ));
197
+
198
+        assertEquals("8", parsedInput);
199
+    }
200
+
201
+    @Ignore
202
+    @Test
203
+    public void testReaderRecognizesSingle9() {
204
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
205
+                " _ ",
206
+                "|_|",
207
+                " _|",
208
+                "   "
209
+        ));
210
+
211
+        assertEquals("9", parsedInput);
212
+    }
213
+
214
+    @Ignore
215
+    @Test
216
+    public void testReaderRecognizesSequence1234567890() {
217
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
218
+                "    _  _     _  _  _  _  _  _ ",
219
+                "  | _| _||_||_ |_   ||_||_|| |",
220
+                "  ||_  _|  | _||_|  ||_| _||_|",
221
+                "                              "
222
+        ));
223
+
224
+        assertEquals("1234567890", parsedInput);
225
+    }
226
+
227
+    @Ignore
228
+    @Test
229
+    public void testReaderRecognizesAndCorrectlyFormatsMultiRowInput() {
230
+        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
231
+                "    _  _ ",
232
+                "  | _| _|",
233
+                "  ||_  _|",
234
+                "         ",
235
+                "    _  _ ",
236
+                "|_||_ |_ ",
237
+                "  | _||_|",
238
+                "         ",
239
+                " _  _  _ ",
240
+                "  ||_||_|",
241
+                "  ||_| _|",
242
+                "         "
243
+        ));
244
+
245
+        assertEquals("123,456,789", parsedInput);
246
+    }
247
+
248
+}

+ 1
- 0
exercises/settings.gradle Целия файл

@@ -32,6 +32,7 @@ include 'meetup'
32 32
 include 'minesweeper'
33 33
 include 'nth-prime'
34 34
 include 'nucleotide-count'
35
+include 'ocr-numbers'
35 36
 include 'octal'
36 37
 include 'palindrome-products'
37 38
 include 'pangram'