-
-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
exercises/spiral-matrix/src/example/java/SpiralMatrixBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
exercises/spiral-matrix/src/test/java/SpiralMatrixBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |