Skip to content

Commit 3a6995d

Browse files
committed
Implemented matrix exercise.
1 parent 54afd5d commit 3a6995d

6 files changed

Lines changed: 244 additions & 2 deletions

File tree

‎config.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"wordy",
5656
"palindrome-products",
5757
"twelve-days",
58-
"sublist"
58+
"sublist",
59+
"matrix"
5960
],
6061
"exercises": [
6162
{
@@ -317,6 +318,11 @@
317318
"slug": "sublist",
318319
"difficulty": 1,
319320
"topics": []
321+
},
322+
{
323+
"slug": "matrix",
324+
"difficulty": 1,
325+
"topics": []
320326
}
321327
],
322328
"deprecated": [

‎exercises/matrix/build.gradle‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Matrix {
2+
private int[][] matrix;
3+
4+
public Matrix(String matrixAsString) {
5+
String[] rows = matrixAsString.split("\\n");
6+
matrix = new int[rows.length][];
7+
for (int i = 0; i < rows.length; i++) {
8+
String[] columnValues = rows[i].split(" ");
9+
matrix[i] = new int[columnValues.length];
10+
for (int j = 0; j < columnValues.length; j++) {
11+
matrix[i][j] = Integer.valueOf(columnValues[j]);
12+
}
13+
}
14+
}
15+
16+
public int[] getRow(int rowNumber) {
17+
return matrix[rowNumber];
18+
}
19+
20+
public int[] getColumn(int columnNumber) {
21+
int[] column = new int[matrix.length];
22+
for(int i = 0; i < matrix.length; i++) {
23+
column[i] = matrix[i][columnNumber];
24+
}
25+
return column;
26+
}
27+
28+
public int getRowsCount() {
29+
return matrix.length;
30+
}
31+
32+
public int getColumnsCount() {
33+
return matrix[0].length;
34+
}
35+
}

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

Whitespace-only changes.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
import org.junit.experimental.runners.Enclosed;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
13+
@RunWith(Enclosed.class)
14+
public class MatrixTest {
15+
16+
@RunWith(Parameterized.class)
17+
public static class GetFirstRowTest {
18+
private Matrix matrix;
19+
private int[] firstRow;
20+
21+
@Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \"{0}\".")
22+
public static Collection<Object[]> data() {
23+
return Arrays.asList(new Object[][]{
24+
{"0", new int[] {0}},
25+
{"0 1", new int[] {0, 1}},
26+
{"0\\n1", new int[] {0}},
27+
{"0 1\\n2 3", new int[] {0, 1}},
28+
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 1, 2}}
29+
});
30+
}
31+
32+
public GetFirstRowTest(String matrixAsString, int[] firstRow) {
33+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
34+
this.firstRow = firstRow;
35+
}
36+
37+
@Test
38+
public void getFirstRowTest() {
39+
assertArrayEquals(matrix.getRow(0), firstRow);
40+
}
41+
}
42+
43+
@RunWith(Parameterized.class)
44+
public static class GetLastRowTest {
45+
private Matrix matrix;
46+
private int[] lastRow;
47+
48+
@Parameterized.Parameters(name = "{index}: checking last row of matrix constructed with string \"{0}\".")
49+
public static Collection<Object[]> data() {
50+
return Arrays.asList(new Object[][]{
51+
{"0", new int[] {0}},
52+
{"0 1", new int[] {0, 1}},
53+
{"0\\n1", new int[] {1}},
54+
{"0 1\\n2 3", new int[] {2, 3}},
55+
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {6, 7, 8}}
56+
});
57+
}
58+
59+
public GetLastRowTest(String matrixAsString, int[] lastRow) {
60+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
61+
this.lastRow = lastRow;
62+
}
63+
64+
@Ignore
65+
@Test
66+
public void getLastRowTest() {
67+
assertArrayEquals(matrix.getRow(matrix.getRowsCount() - 1), lastRow);
68+
}
69+
}
70+
71+
@RunWith(Parameterized.class)
72+
public static class GetFirstColumnTest {
73+
private Matrix matrix;
74+
private int[] firstColumn;
75+
76+
@Parameterized.Parameters(name = "{index}: checking first column of matrix constructed with string \"{0}\".")
77+
public static Collection<Object[]> data() {
78+
return Arrays.asList(new Object[][]{
79+
{"0", new int[] {0}},
80+
{"0 1", new int[] {0}},
81+
{"0\\n1", new int[] {0, 1}},
82+
{"0 1\\n2 3", new int[] {0, 2}},
83+
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 3, 6}}
84+
});
85+
}
86+
87+
public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
88+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
89+
this.firstColumn = firstColumn;
90+
}
91+
92+
@Ignore
93+
@Test
94+
public void getFirstColumnTest() {
95+
assertArrayEquals(matrix.getColumn(0), firstColumn);
96+
}
97+
}
98+
99+
100+
@RunWith(Parameterized.class)
101+
public static class GetLastColumnTest {
102+
private Matrix matrix;
103+
private int[] lastColumn;
104+
105+
@Parameterized.Parameters(name = "{index}: checking last column of matrix constructed with string \"{0}\".")
106+
public static Collection<Object[]> data() {
107+
return Arrays.asList(new Object[][]{
108+
{"0", new int[] {0}},
109+
{"0 1", new int[] {1}},
110+
{"0\\n1", new int[] {0, 1}},
111+
{"0 1\\n2 3", new int[] {1, 3}},
112+
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {2, 5, 8}}
113+
});
114+
}
115+
116+
public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
117+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
118+
this.lastColumn = lastColumn;
119+
}
120+
121+
@Ignore
122+
@Test
123+
public void getLastColumnTest() {
124+
assertArrayEquals(matrix.getColumn(matrix.getColumnsCount() - 1), lastColumn);
125+
}
126+
}
127+
128+
@RunWith(Parameterized.class)
129+
public static class CountColumnsTest {
130+
private Matrix matrix;
131+
private int numberOfColumns;
132+
133+
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} column(s).")
134+
public static Collection<Object[]> data() {
135+
return Arrays.asList(new Object[][]{
136+
{"0", 1},
137+
{"0 1", 2},
138+
{"0\\n1", 1},
139+
{"0 1\\n2 3", 2},
140+
{"0 1 2\\n3 4 5\\n6 7 8", 3}
141+
});
142+
}
143+
144+
public CountColumnsTest(String matrixAsString, int numberOfColumns) {
145+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
146+
this.numberOfColumns = numberOfColumns;
147+
}
148+
149+
@Ignore
150+
@Test
151+
public void countColumnsTest() {
152+
assertEquals(matrix.getColumnsCount(), numberOfColumns);
153+
}
154+
}
155+
156+
@RunWith(Parameterized.class)
157+
public static class CountRowsTest {
158+
private Matrix matrix;
159+
private int numberOfRows;
160+
161+
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} row(s).")
162+
public static Collection<Object[]> data() {
163+
return Arrays.asList(new Object[][]{
164+
{"0", 1},
165+
{"0 1", 1},
166+
{"0\\n1", 2},
167+
{"0 1\\n2 3", 2},
168+
{"0 1 2\\n3 4 5\\n6 7 8", 3}
169+
});
170+
}
171+
172+
public CountRowsTest(String matrixAsString, int numberOfRows) {
173+
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
174+
this.numberOfRows = numberOfRows;
175+
}
176+
177+
@Ignore
178+
@Test
179+
public void countRowsTest() {
180+
assertEquals(matrix.getRowsCount(), numberOfRows);
181+
}
182+
}
183+
184+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ include 'trinary'
5353
include 'word-count'
5454
include 'wordy'
5555
include 'twelve-days'
56-
56+
include 'matrix'

0 commit comments

Comments
 (0)