Просмотр исходного кода

Merge pull request #627 from exercism/saddle-pts-cleanup

saddle-points: API cleanup, ordering
FridaTveit 9 лет назад
Родитель
Сommit
e32ba10473

+ 5
- 5
config.json Просмотреть файл

@@ -111,6 +111,11 @@
111 111
       "topics": []
112 112
     },
113 113
     {
114
+      "slug": "saddle-points",
115
+      "difficulty": 4,
116
+      "topics": []
117
+    },
118
+    {
114 119
       "slug": "diamond",
115 120
       "difficulty": 4,
116 121
       "topics": []
@@ -321,11 +326,6 @@
321 326
       "topics": []
322 327
     },
323 328
     {
324
-      "slug": "saddle-points",
325
-      "difficulty": 1,
326
-      "topics": []
327
-    },
328
-    {
329 329
       "slug": "run-length-encoding",
330 330
       "difficulty": 1,
331 331
       "topics": []

+ 5
- 4
exercises/saddle-points/src/example/java/Matrix.java Просмотреть файл

@@ -1,6 +1,7 @@
1
-import java.util.ArrayList;
2 1
 import java.util.Collections;
2
+import java.util.HashSet;
3 3
 import java.util.List;
4
+import java.util.Set;
4 5
 
5 6
 final class Matrix {
6 7
 
@@ -10,8 +11,8 @@ final class Matrix {
10 11
     this.values = values;
11 12
   }
12 13
 
13
-  List<MatrixCoordinate> getSaddlePoints() {
14
-    final List<MatrixCoordinate> result = new ArrayList<>();
14
+  Set<MatrixCoordinate> getSaddlePoints() {
15
+    final Set<MatrixCoordinate> result = new HashSet<>();
15 16
 
16 17
     if (values.isEmpty()) {
17 18
       return result;
@@ -38,7 +39,7 @@ final class Matrix {
38 39
     return values.stream()
39 40
         .map(row -> row.get(column))
40 41
         .min(Integer::compareTo)
41
-        .get();
42
+        .orElseThrow(() -> new IllegalArgumentException("Column cannot be empty"));
42 43
   }
43 44
 
44 45
 }

+ 5
- 5
exercises/saddle-points/src/example/java/MatrixCoordinate.java Просмотреть файл

@@ -2,11 +2,11 @@ final class MatrixCoordinate {
2 2
 
3 3
   private final int row;
4 4
 
5
-  private final int column;
5
+  private final int col;
6 6
 
7
-  MatrixCoordinate(final int row, final int column) {
7
+  MatrixCoordinate(final int row, final int col) {
8 8
     this.row = row;
9
-    this.column = column;
9
+    this.col = col;
10 10
   }
11 11
 
12 12
   // Generated equals and hashcode.
@@ -18,13 +18,13 @@ final class MatrixCoordinate {
18 18
 
19 19
     final MatrixCoordinate that = (MatrixCoordinate) o;
20 20
 
21
-    return row == that.row && column == that.column;
21
+    return row == that.row && col == that.col;
22 22
   }
23 23
 
24 24
   @Override
25 25
   public int hashCode() {
26 26
     int result = row;
27
-    result = 31 * result + column;
27
+    result = 31 * result + col;
28 28
     return result;
29 29
   }
30 30
 

+ 5
- 5
exercises/saddle-points/src/main/java/MatrixCoordinate.java Просмотреть файл

@@ -2,11 +2,11 @@ final class MatrixCoordinate {
2 2
 
3 3
   private final int row;
4 4
 
5
-  private final int column;
5
+  private final int col;
6 6
 
7
-  MatrixCoordinate(final int row, final int column) {
7
+  MatrixCoordinate(final int row, final int col) {
8 8
     this.row = row;
9
-    this.column = column;
9
+    this.col = col;
10 10
   }
11 11
 
12 12
   // Generated equals and hashcode.
@@ -18,13 +18,13 @@ final class MatrixCoordinate {
18 18
 
19 19
     final MatrixCoordinate that = (MatrixCoordinate) o;
20 20
 
21
-    return row == that.row && column == that.column;
21
+    return row == that.row && col == that.col;
22 22
   }
23 23
 
24 24
   @Override
25 25
   public int hashCode() {
26 26
     int result = row;
27
-    result = 31 * result + column;
27
+    result = 31 * result + col;
28 28
     return result;
29 29
   }
30 30
 

+ 10
- 10
exercises/saddle-points/src/test/java/MatrixTest.java Просмотреть файл

@@ -1,13 +1,13 @@
1 1
 import org.junit.Ignore;
2 2
 import org.junit.Test;
3 3
 
4
-import java.util.ArrayList;
5
-import java.util.Arrays;
6
-import java.util.Collections;
7
-import java.util.List;
4
+import java.util.*;
8 5
 
9 6
 import static org.junit.Assert.assertEquals;
10 7
 
8
+/*
9
+ * version: 1.0.0
10
+ */
11 11
 public class MatrixTest {
12 12
 
13 13
     @Test
@@ -18,7 +18,7 @@ public class MatrixTest {
18 18
             Arrays.asList(6, 6, 7)
19 19
         ));
20 20
 
21
-        List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(1, 0));
21
+        Set<MatrixCoordinate> expectedSaddlePoints = Collections.singleton(new MatrixCoordinate(1, 0));
22 22
 
23 23
         assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
24 24
     }
@@ -28,7 +28,7 @@ public class MatrixTest {
28 28
     public void testCanIdentifyThatEmptyMatrixHasNoSaddlePoints() {
29 29
         Matrix matrix = new Matrix(new ArrayList<>());
30 30
 
31
-        List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
31
+        Set<MatrixCoordinate> expectedSaddlePoints = Collections.emptySet();
32 32
 
33 33
         assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
34 34
     }
@@ -42,7 +42,7 @@ public class MatrixTest {
42 42
             Arrays.asList(2, 3, 1)
43 43
         ));
44 44
 
45
-        List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
45
+        Set<MatrixCoordinate> expectedSaddlePoints = Collections.emptySet();
46 46
 
47 47
         assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
48 48
     }
@@ -56,11 +56,11 @@ public class MatrixTest {
56 56
             Arrays.asList(1, 5, 4)
57 57
         ));
58 58
 
59
-        List<MatrixCoordinate> expectedSaddlePoints = Arrays.asList(
59
+        Set<MatrixCoordinate> expectedSaddlePoints = new HashSet<>(Arrays.asList(
60 60
             new MatrixCoordinate(0, 1),
61 61
             new MatrixCoordinate(1, 1),
62 62
             new MatrixCoordinate(2, 1)
63
-        );
63
+        ));
64 64
 
65 65
         assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
66 66
     }
@@ -74,7 +74,7 @@ public class MatrixTest {
74 74
             Arrays.asList(3, 2, 5)
75 75
         ));
76 76
 
77
-        List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(2, 2));
77
+        Set<MatrixCoordinate> expectedSaddlePoints = Collections.singleton(new MatrixCoordinate(2, 2));
78 78
 
79 79
         assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
80 80
     }