Skip to content

Commit f23d3e0

Browse files
committed
spiral-matrix: add to track
1 parent 278e7f7 commit f23d3e0

8 files changed

Lines changed: 201 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@
284284

285285
]
286286
},
287+
{
288+
"slug": "spiral-matrix",
289+
"difficulty": 6,
290+
"topics": [
291+
292+
]
293+
},
287294
{
288295
"slug": "roman-numerals",
289296
"difficulty": 6,

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ include 'sieve'
6565
include 'simple-cipher'
6666
include 'simple-linked-list'
6767
include 'space-age'
68+
include 'spiral-matrix'
6869
include 'strain'
6970
include 'sublist'
7071
include 'sum-of-multiples'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎exercises/spiral-matrix/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)