Skip to content

Commit

Permalink
spiral-matrix: add to track
Browse files Browse the repository at this point in the history
  • Loading branch information
stkent committed Jul 3, 2017
1 parent 278e7f7 commit f23d3e0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@

]
},
{
"slug": "spiral-matrix",
"difficulty": 6,
"topics": [

]
},
{
"slug": "roman-numerals",
"difficulty": 6,
Expand Down
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions exercises/spiral-matrix/build.gradle
Original file line number Diff line number Diff line change
@@ -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"]
}
}
31 changes: 31 additions & 0 deletions exercises/spiral-matrix/src/example/java/Coordinate.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
29 changes: 29 additions & 0 deletions exercises/spiral-matrix/src/example/java/Direction.java
Original file line number Diff line number Diff line change
@@ -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];
}

}
29 changes: 29 additions & 0 deletions exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Empty file.
86 changes: 86 additions & 0 deletions exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit f23d3e0

Please sign in to comment.