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

Merge pull request #677 from exercism/spiral-matrix

spiral-matrix: add to track
FridaTveit 9 лет назад
Родитель
Сommit
0c24d30c8d

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

@@ -285,6 +285,13 @@
285 285
       ]
286 286
     },
287 287
     {
288
+      "slug": "spiral-matrix",
289
+      "difficulty": 6,
290
+      "topics": [
291
+
292
+      ]
293
+    },
294
+    {
288 295
       "slug": "roman-numerals",
289 296
       "difficulty": 6,
290 297
       "topics": [

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

@@ -65,6 +65,7 @@ include 'sieve'
65 65
 include 'simple-cipher'
66 66
 include 'simple-linked-list'
67 67
 include 'space-age'
68
+include 'spiral-matrix'
68 69
 include 'strain'
69 70
 include 'sublist'
70 71
 include 'sum-of-multiples'

+ 18
- 0
exercises/spiral-matrix/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
+}
12
+
13
+test {
14
+  testLogging {
15
+    exceptionFormat = 'full'
16
+    events = ["passed", "failed", "skipped"]
17
+  }
18
+}

+ 31
- 0
exercises/spiral-matrix/src/example/java/Coordinate.java Просмотреть файл

@@ -0,0 +1,31 @@
1
+class Coordinate {
2
+
3
+    private final int x;
4
+
5
+    private final int y;
6
+
7
+    Coordinate(int x, int y) {
8
+        this.x = x;
9
+        this.y = y;
10
+    }
11
+
12
+    int getX() {
13
+        return x;
14
+    }
15
+
16
+    int getY() {
17
+        return y;
18
+    }
19
+
20
+    Coordinate step(Direction direction) {
21
+        return new Coordinate(x + direction.getDx(), y + direction.getDy());
22
+    }
23
+
24
+    boolean isWithinGridOfSize(int size) {
25
+        return x >= 0
26
+                && x < size
27
+                && y >= 0
28
+                && y < size;
29
+    }
30
+
31
+}

+ 29
- 0
exercises/spiral-matrix/src/example/java/Direction.java Просмотреть файл

@@ -0,0 +1,29 @@
1
+enum Direction {
2
+
3
+    UP   ( 0, -1),
4
+    RIGHT( 1,  0),
5
+    DOWN ( 0,  1),
6
+    LEFT (-1,  0);
7
+
8
+    private final int dx;
9
+
10
+    private final int dy;
11
+
12
+    Direction(int dx, int dy) {
13
+        this.dx = dx;
14
+        this.dy = dy;
15
+    }
16
+
17
+    int getDx() {
18
+        return dx;
19
+    }
20
+
21
+    int getDy() {
22
+        return dy;
23
+    }
24
+
25
+    Direction turnRight() {
26
+        return Direction.values()[(ordinal() + 1) % Direction.values().length];
27
+    }
28
+
29
+}

+ 29
- 0
exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java Просмотреть файл

@@ -0,0 +1,29 @@
1
+class SpiralMatrixBuilder {
2
+
3
+    int[][] buildMatrixOfSize(int size) {
4
+        if (size == 0) {
5
+            return new int[][]{};
6
+        }
7
+
8
+        int[][] result = new int[size][size];
9
+        int entryCount = (int) Math.pow(size, 2.0);
10
+
11
+        Coordinate coord = new Coordinate(0, 0);
12
+        Direction direction = Direction.RIGHT;
13
+
14
+        for (int i = 0; i < entryCount; i++) {
15
+            result[coord.getY()][coord.getX()] = i + 1;
16
+
17
+            Coordinate maybeNextCoord = coord.step(direction);
18
+            if (maybeNextCoord.isWithinGridOfSize(size) && result[maybeNextCoord.getY()][maybeNextCoord.getX()] == 0) {
19
+                coord = maybeNextCoord;
20
+            } else {
21
+                direction = direction.turnRight();
22
+                coord = coord.step(direction);
23
+            }
24
+        }
25
+
26
+        return result;
27
+    }
28
+
29
+}

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


+ 86
- 0
exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java Просмотреть файл

@@ -0,0 +1,86 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertArrayEquals;
6
+
7
+/*
8
+ * version: 1.0.0
9
+ */
10
+public class SpiralMatrixBuilderTest {
11
+
12
+    private SpiralMatrixBuilder spiralMatrixBuilder;
13
+
14
+    @Before
15
+    public void setUp() {
16
+        spiralMatrixBuilder = new SpiralMatrixBuilder();
17
+    }
18
+
19
+    @Test
20
+    public void testEmptySpiral() {
21
+        int[][] expected = {};
22
+
23
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(0));
24
+    }
25
+
26
+    @Ignore("Remove to run test")
27
+    @Test
28
+    public void testTrivialSpiral() {
29
+        int[][] expected = {
30
+                {1}
31
+        };
32
+
33
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(1));
34
+    }
35
+
36
+    @Ignore("Remove to run test")
37
+    @Test
38
+    public void testSpiralOfSize2() {
39
+        int[][] expected = {
40
+                {1, 2},
41
+                {4, 3}
42
+        };
43
+
44
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(2));
45
+    }
46
+
47
+    @Ignore("Remove to run test")
48
+    @Test
49
+    public void testSpiralOfSize3() {
50
+        int[][] expected = {
51
+                {1, 2, 3},
52
+                {8, 9, 4},
53
+                {7, 6, 5}
54
+        };
55
+
56
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(3));
57
+    }
58
+
59
+    @Ignore("Remove to run test")
60
+    @Test
61
+    public void testSpiralOfSize4() {
62
+        int[][] expected = {
63
+                { 1,  2,  3,  4},
64
+                {12, 13, 14,  5},
65
+                {11, 16, 15,  6},
66
+                {10,  9,  8,  7}
67
+        };
68
+
69
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(4));
70
+    }
71
+
72
+    @Ignore("Remove to run test")
73
+    @Test
74
+    public void testSpiralOfSize5() {
75
+        int[][] expected = {
76
+                { 1,  2,  3,  4,  5},
77
+                {16, 17, 18, 19,  6},
78
+                {15, 24, 25, 20,  7},
79
+                {14, 23, 22, 21,  8},
80
+                {13, 12, 11, 10,  9}
81
+        };
82
+
83
+        assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(5));
84
+    }
85
+
86
+}