浏览代码

Implemented matrix exercise.

Frida Tveit 9 年前
父节点
当前提交
3a6995db36

+ 7
- 1
config.json 查看文件

@@ -55,7 +55,8 @@
55 55
     "wordy",
56 56
     "palindrome-products",
57 57
     "twelve-days",
58
-    "sublist"
58
+    "sublist",
59
+    "matrix"
59 60
   ],
60 61
   "exercises": [
61 62
     {
@@ -317,6 +318,11 @@
317 318
       "slug": "sublist",
318 319
       "difficulty": 1,
319 320
       "topics": []
321
+    },
322
+    {
323
+      "slug": "matrix",
324
+      "difficulty": 1,
325
+      "topics": []
320 326
     }
321 327
   ],
322 328
   "deprecated": [

+ 17
- 0
exercises/matrix/build.gradle 查看文件

@@ -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
+}

+ 35
- 0
exercises/matrix/src/example/java/Matrix.java 查看文件

@@ -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
+}

+ 0
- 0
exercises/matrix/src/main/java/.keep 查看文件


+ 184
- 0
exercises/matrix/src/test/java/MatrixTest.java 查看文件

@@ -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
+}

+ 1
- 1
exercises/settings.gradle 查看文件

@@ -53,4 +53,4 @@ include 'trinary'
53 53
 include 'word-count'
54 54
 include 'wordy'
55 55
 include 'twelve-days'
56
-
56
+include 'matrix'