Procházet zdrojové kódy

diamond: add to track (#256)

Stuart Kent před 9 roky
rodič
revize
5293a3b977

+ 7
- 1
config.json Zobrazit soubor

57
     "twelve-days",
57
     "twelve-days",
58
     "sublist",
58
     "sublist",
59
     "rectangles",
59
     "rectangles",
60
-    "matrix"
60
+    "matrix",
61
+    "diamond"
61
   ],
62
   ],
62
   "exercises": [
63
   "exercises": [
63
     {
64
     {
329
       "slug": "matrix",
330
       "slug": "matrix",
330
       "difficulty": 1,
331
       "difficulty": 1,
331
       "topics": []
332
       "topics": []
333
+    },
334
+    {
335
+      "slug": "diamond",
336
+      "difficulty": 1,
337
+      "topics": []
332
     }
338
     }
333
   ],
339
   ],
334
   "deprecated": [
340
   "deprecated": [

+ 17
- 0
exercises/diamond/build.gradle Zobrazit soubor

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
+}

+ 41
- 0
exercises/diamond/src/example/java/DiamondPrinter.java Zobrazit soubor

1
+import java.util.ArrayList;
2
+import java.util.Collections;
3
+import java.util.List;
4
+
5
+final class DiamondPrinter {
6
+
7
+    private static final int A_INT = (int) 'A';
8
+
9
+    private static String blank(final int length) {
10
+        return String.join("", Collections.nCopies(length, " "));
11
+    }
12
+
13
+    private static String reverse(final String string) {
14
+        return new StringBuilder(string).reverse().toString();
15
+    }
16
+
17
+    List<String> printToList(final char chr) {
18
+        final int nRows = 2 * ((int) chr - A_INT) + 1;
19
+
20
+        final List<String> topRows = new ArrayList<>();
21
+
22
+        // Populate the top rows.
23
+        for (int nRow = 0; nRow < (nRows + 1) / 2; nRow++) {
24
+            final char rowChr = (char) (A_INT + nRow);
25
+
26
+            final String leftHalfOfRow  = blank((nRows - 1) / 2 - nRow) + rowChr + blank(nRow);
27
+            final String rightHalfOfRow = reverse(leftHalfOfRow.substring(0, leftHalfOfRow.length() - 1));
28
+            final String fullRow        = leftHalfOfRow + rightHalfOfRow;
29
+
30
+            topRows.add(fullRow);
31
+        }
32
+
33
+        // Populate the bottom rows by 'reflecting' the top rows.
34
+        for (int nRow = (nRows - 1) / 2 - 1; nRow >= 0; nRow--) {
35
+            topRows.add(topRows.get(nRow));
36
+        }
37
+
38
+        return topRows;
39
+    }
40
+
41
+}

+ 5
- 0
exercises/diamond/src/main/java/DiamondPrinter.java Zobrazit soubor

1
+final class DiamondPrinter {
2
+
3
+
4
+
5
+}

+ 111
- 0
exercises/diamond/src/test/java/DiamondPrinterTest.java Zobrazit soubor

1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import java.util.List;
5
+
6
+import static java.util.Arrays.asList;
7
+import static java.util.Collections.singletonList;
8
+import static org.hamcrest.CoreMatchers.is;
9
+import static org.junit.Assert.assertThat;
10
+
11
+public final class DiamondPrinterTest {
12
+
13
+    @Test
14
+    public void testOneByOneDiamond() {
15
+        List<String> output = new DiamondPrinter().printToList('A');
16
+        assertThat(output, is(singletonList("A")));
17
+    }
18
+
19
+    @Ignore
20
+    @Test
21
+    public void testTwoByTwoDiamond() {
22
+        List<String> output = new DiamondPrinter().printToList('B');
23
+        assertThat(output, is(asList(" A ",
24
+                                     "B B",
25
+                                     " A ")));
26
+    }
27
+
28
+    @Ignore
29
+    @Test
30
+    public void testThreeByThreeDiamond() {
31
+        List<String> output = new DiamondPrinter().printToList('C');
32
+        assertThat(output, is(asList("  A  ",
33
+                                     " B B ",
34
+                                     "C   C",
35
+                                     " B B ",
36
+                                     "  A  ")));
37
+    }
38
+
39
+    @Ignore
40
+    @Test
41
+    public void testFiveByFiveDiamond() {
42
+        List<String> output = new DiamondPrinter().printToList('E');
43
+        assertThat(output, is(asList("    A    ",
44
+                                     "   B B   ",
45
+                                     "  C   C  ",
46
+                                     " D     D ",
47
+                                     "E       E",
48
+                                     " D     D ",
49
+                                     "  C   C  ",
50
+                                     "   B B   ",
51
+                                     "    A    ")));
52
+    }
53
+
54
+    @Ignore
55
+    @Test
56
+    public void testFullDiamond() {
57
+        List<String> output = new DiamondPrinter().printToList('Z');
58
+        assertThat(output, is(asList("                         A                         ",
59
+                                     "                        B B                        ",
60
+                                     "                       C   C                       ",
61
+                                     "                      D     D                      ",
62
+                                     "                     E       E                     ",
63
+                                     "                    F         F                    ",
64
+                                     "                   G           G                   ",
65
+                                     "                  H             H                  ",
66
+                                     "                 I               I                 ",
67
+                                     "                J                 J                ",
68
+                                     "               K                   K               ",
69
+                                     "              L                     L              ",
70
+                                     "             M                       M             ",
71
+                                     "            N                         N            ",
72
+                                     "           O                           O           ",
73
+                                     "          P                             P          ",
74
+                                     "         Q                               Q         ",
75
+                                     "        R                                 R        ",
76
+                                     "       S                                   S       ",
77
+                                     "      T                                     T      ",
78
+                                     "     U                                       U     ",
79
+                                     "    V                                         V    ",
80
+                                     "   W                                           W   ",
81
+                                     "  X                                             X  ",
82
+                                     " Y                                               Y ",
83
+                                     "Z                                                 Z",
84
+                                     " Y                                               Y ",
85
+                                     "  X                                             X  ",
86
+                                     "   W                                           W   ",
87
+                                     "    V                                         V    ",
88
+                                     "     U                                       U     ",
89
+                                     "      T                                     T      ",
90
+                                     "       S                                   S       ",
91
+                                     "        R                                 R        ",
92
+                                     "         Q                               Q         ",
93
+                                     "          P                             P          ",
94
+                                     "           O                           O           ",
95
+                                     "            N                         N            ",
96
+                                     "             M                       M             ",
97
+                                     "              L                     L              ",
98
+                                     "               K                   K               ",
99
+                                     "                J                 J                ",
100
+                                     "                 I               I                 ",
101
+                                     "                  H             H                  ",
102
+                                     "                   G           G                   ",
103
+                                     "                    F         F                    ",
104
+                                     "                     E       E                     ",
105
+                                     "                      D     D                      ",
106
+                                     "                       C   C                       ",
107
+                                     "                        B B                        ",
108
+                                     "                         A                         ")));
109
+    }
110
+
111
+}

+ 1
- 0
exercises/settings.gradle Zobrazit soubor

12
 include 'bracket-push'
12
 include 'bracket-push'
13
 include 'crypto-square'
13
 include 'crypto-square'
14
 include 'custom-set'
14
 include 'custom-set'
15
+include 'diamond'
15
 include 'difference-of-squares'
16
 include 'difference-of-squares'
16
 include 'etl'
17
 include 'etl'
17
 include 'gigasecond'
18
 include 'gigasecond'