Skip to content

Commit 63020d4

Browse files
authored
Merge pull request exercism#244 from FridaTveit/MatrixExercise
dibs: I will implement exercise Matrix
2 parents 5422e53 + 0ba73c6 commit 63020d4

6 files changed

Lines changed: 271 additions & 2 deletions

File tree

‎config.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"palindrome-products",
5757
"twelve-days",
5858
"sublist",
59-
"rectangles"
59+
"rectangles",
60+
"matrix"
6061
],
6162
"exercises": [
6263
{
@@ -323,6 +324,11 @@
323324
"slug": "rectangles",
324325
"difficulty": 1,
325326
"topics": []
327+
},
328+
{
329+
"slug": "matrix",
330+
"difficulty": 1,
331+
"topics": []
326332
}
327333
],
328334
"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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.regex.Pattern;
2+
3+
public class Matrix {
4+
private int[][] matrix;
5+
private static Pattern spacePattern = Pattern.compile(" ");
6+
private static Pattern newlinePattern = Pattern.compile("\\n");
7+
8+
public Matrix(String matrixAsString) {
9+
String[] rows = newlinePattern.split(matrixAsString);
10+
matrix = new int[rows.length][];
11+
for (int i = 0; i < rows.length; i++) {
12+
String[] columnValues = spacePattern.split(rows[i]);
13+
matrix[i] = new int[columnValues.length];
14+
for (int j = 0; j < columnValues.length; j++) {
15+
matrix[i][j] = Integer.parseInt(columnValues[j]);
16+
}
17+
}
18+
}
19+
20+
public int[] getRow(int rowNumber) {
21+
return matrix[rowNumber];
22+
}
23+
24+
public int[] getColumn(int columnNumber) {
25+
int[] column = new int[matrix.length];
26+
for(int i = 0; i < matrix.length; i++) {
27+
column[i] = matrix[i][columnNumber];
28+
}
29+
return column;
30+
}
31+
32+
public int getRowsCount() {
33+
return matrix.length;
34+
}
35+
36+
public int getColumnsCount() {
37+
return matrix[0].length;
38+
}
39+
}

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

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

‎exercises/settings.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ include 'trinary'
5555
include 'word-count'
5656
include 'wordy'
5757
include 'twelve-days'
58-
58+
include 'matrix'

0 commit comments

Comments
 (0)