Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spiral-matrix: add to track #677

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best guess.

"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));
}

}