diff --git a/config.json b/config.json index 29b6a7d90..70bc350a4 100644 --- a/config.json +++ b/config.json @@ -284,6 +284,13 @@ ] }, + { + "slug": "spiral-matrix", + "difficulty": 6, + "topics": [ + + ] + }, { "slug": "roman-numerals", "difficulty": 6, diff --git a/exercises/settings.gradle b/exercises/settings.gradle index b5db7e468..10ca082c8 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -65,6 +65,7 @@ include 'sieve' include 'simple-cipher' include 'simple-linked-list' include 'space-age' +include 'spiral-matrix' include 'strain' include 'sublist' include 'sum-of-multiples' diff --git a/exercises/spiral-matrix/build.gradle b/exercises/spiral-matrix/build.gradle new file mode 100644 index 000000000..019e5f323 --- /dev/null +++ b/exercises/spiral-matrix/build.gradle @@ -0,0 +1,18 @@ +apply plugin: "java" +apply plugin: "eclipse" +apply plugin: "idea" + +repositories { + mavenCentral() +} + +dependencies { + testCompile "junit:junit:4.12" +} + +test { + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/spiral-matrix/src/example/java/Coordinate.java b/exercises/spiral-matrix/src/example/java/Coordinate.java new file mode 100644 index 000000000..1044023be --- /dev/null +++ b/exercises/spiral-matrix/src/example/java/Coordinate.java @@ -0,0 +1,31 @@ +class Coordinate { + + private final int x; + + private final int y; + + Coordinate(int x, int y) { + this.x = x; + this.y = y; + } + + int getX() { + return x; + } + + int getY() { + return y; + } + + Coordinate step(Direction direction) { + return new Coordinate(x + direction.getDx(), y + direction.getDy()); + } + + boolean isWithinGridOfSize(int size) { + return x >= 0 + && x < size + && y >= 0 + && y < size; + } + +} diff --git a/exercises/spiral-matrix/src/example/java/Direction.java b/exercises/spiral-matrix/src/example/java/Direction.java new file mode 100644 index 000000000..3fe1c7cb4 --- /dev/null +++ b/exercises/spiral-matrix/src/example/java/Direction.java @@ -0,0 +1,29 @@ +enum Direction { + + UP ( 0, -1), + RIGHT( 1, 0), + DOWN ( 0, 1), + LEFT (-1, 0); + + private final int dx; + + private final int dy; + + Direction(int dx, int dy) { + this.dx = dx; + this.dy = dy; + } + + int getDx() { + return dx; + } + + int getDy() { + return dy; + } + + Direction turnRight() { + return Direction.values()[(ordinal() + 1) % Direction.values().length]; + } + +} diff --git a/exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java b/exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java new file mode 100644 index 000000000..b24f0d781 --- /dev/null +++ b/exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java @@ -0,0 +1,29 @@ +class SpiralMatrixBuilder { + + int[][] buildMatrixOfSize(int size) { + if (size == 0) { + return new int[][]{}; + } + + int[][] result = new int[size][size]; + int entryCount = (int) Math.pow(size, 2.0); + + Coordinate coord = new Coordinate(0, 0); + Direction direction = Direction.RIGHT; + + for (int i = 0; i < entryCount; i++) { + result[coord.getY()][coord.getX()] = i + 1; + + Coordinate maybeNextCoord = coord.step(direction); + if (maybeNextCoord.isWithinGridOfSize(size) && result[maybeNextCoord.getY()][maybeNextCoord.getX()] == 0) { + coord = maybeNextCoord; + } else { + direction = direction.turnRight(); + coord = coord.step(direction); + } + } + + return result; + } + +} diff --git a/exercises/spiral-matrix/src/main/java/.keep b/exercises/spiral-matrix/src/main/java/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java b/exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java new file mode 100644 index 000000000..ba2328660 --- /dev/null +++ b/exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java @@ -0,0 +1,86 @@ +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; + +/* + * version: 1.0.0 + */ +public class SpiralMatrixBuilderTest { + + private SpiralMatrixBuilder spiralMatrixBuilder; + + @Before + public void setUp() { + spiralMatrixBuilder = new SpiralMatrixBuilder(); + } + + @Test + public void testEmptySpiral() { + int[][] expected = {}; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(0)); + } + + @Ignore("Remove to run test") + @Test + public void testTrivialSpiral() { + int[][] expected = { + {1} + }; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(1)); + } + + @Ignore("Remove to run test") + @Test + public void testSpiralOfSize2() { + int[][] expected = { + {1, 2}, + {4, 3} + }; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(2)); + } + + @Ignore("Remove to run test") + @Test + public void testSpiralOfSize3() { + int[][] expected = { + {1, 2, 3}, + {8, 9, 4}, + {7, 6, 5} + }; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(3)); + } + + @Ignore("Remove to run test") + @Test + public void testSpiralOfSize4() { + int[][] expected = { + { 1, 2, 3, 4}, + {12, 13, 14, 5}, + {11, 16, 15, 6}, + {10, 9, 8, 7} + }; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(4)); + } + + @Ignore("Remove to run test") + @Test + public void testSpiralOfSize5() { + int[][] expected = { + { 1, 2, 3, 4, 5}, + {16, 17, 18, 19, 6}, + {15, 24, 25, 20, 7}, + {14, 23, 22, 21, 8}, + {13, 12, 11, 10, 9} + }; + + assertArrayEquals(expected, spiralMatrixBuilder.buildMatrixOfSize(5)); + } + +}