Skip to content

Commit e32ba10

Browse files
authored
Merge pull request exercism#627 from exercism/saddle-pts-cleanup
saddle-points: API cleanup, ordering
2 parents 9d069f2 + 8223868 commit e32ba10

5 files changed

Lines changed: 30 additions & 29 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110
"difficulty": 4,
111111
"topics": []
112112
},
113+
{
114+
"slug": "saddle-points",
115+
"difficulty": 4,
116+
"topics": []
117+
},
113118
{
114119
"slug": "diamond",
115120
"difficulty": 4,
@@ -320,11 +325,6 @@
320325
"difficulty": 1,
321326
"topics": []
322327
},
323-
{
324-
"slug": "saddle-points",
325-
"difficulty": 1,
326-
"topics": []
327-
},
328328
{
329329
"slug": "run-length-encoding",
330330
"difficulty": 1,

‎exercises/saddle-points/src/example/java/Matrix.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import java.util.ArrayList;
21
import java.util.Collections;
2+
import java.util.HashSet;
33
import java.util.List;
4+
import java.util.Set;
45

56
final class Matrix {
67

@@ -10,8 +11,8 @@ final class Matrix {
1011
this.values = values;
1112
}
1213

13-
List<MatrixCoordinate> getSaddlePoints() {
14-
final List<MatrixCoordinate> result = new ArrayList<>();
14+
Set<MatrixCoordinate> getSaddlePoints() {
15+
final Set<MatrixCoordinate> result = new HashSet<>();
1516

1617
if (values.isEmpty()) {
1718
return result;
@@ -38,7 +39,7 @@ private int getColumnMin(final int column) {
3839
return values.stream()
3940
.map(row -> row.get(column))
4041
.min(Integer::compareTo)
41-
.get();
42+
.orElseThrow(() -> new IllegalArgumentException("Column cannot be empty"));
4243
}
4344

4445
}

‎exercises/saddle-points/src/example/java/MatrixCoordinate.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ final class MatrixCoordinate {
22

33
private final int row;
44

5-
private final int column;
5+
private final int col;
66

7-
MatrixCoordinate(final int row, final int column) {
7+
MatrixCoordinate(final int row, final int col) {
88
this.row = row;
9-
this.column = column;
9+
this.col = col;
1010
}
1111

1212
// Generated equals and hashcode.
@@ -18,13 +18,13 @@ public boolean equals(final Object o) {
1818

1919
final MatrixCoordinate that = (MatrixCoordinate) o;
2020

21-
return row == that.row && column == that.column;
21+
return row == that.row && col == that.col;
2222
}
2323

2424
@Override
2525
public int hashCode() {
2626
int result = row;
27-
result = 31 * result + column;
27+
result = 31 * result + col;
2828
return result;
2929
}
3030

‎exercises/saddle-points/src/main/java/MatrixCoordinate.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ final class MatrixCoordinate {
22

33
private final int row;
44

5-
private final int column;
5+
private final int col;
66

7-
MatrixCoordinate(final int row, final int column) {
7+
MatrixCoordinate(final int row, final int col) {
88
this.row = row;
9-
this.column = column;
9+
this.col = col;
1010
}
1111

1212
// Generated equals and hashcode.
@@ -18,13 +18,13 @@ public boolean equals(final Object o) {
1818

1919
final MatrixCoordinate that = (MatrixCoordinate) o;
2020

21-
return row == that.row && column == that.column;
21+
return row == that.row && col == that.col;
2222
}
2323

2424
@Override
2525
public int hashCode() {
2626
int result = row;
27-
result = 31 * result + column;
27+
result = 31 * result + col;
2828
return result;
2929
}
3030

‎exercises/saddle-points/src/test/java/MatrixTest.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import org.junit.Ignore;
22
import org.junit.Test;
33

4-
import java.util.ArrayList;
5-
import java.util.Arrays;
6-
import java.util.Collections;
7-
import java.util.List;
4+
import java.util.*;
85

96
import static org.junit.Assert.assertEquals;
107

8+
/*
9+
* version: 1.0.0
10+
*/
1111
public class MatrixTest {
1212

1313
@Test
@@ -18,7 +18,7 @@ public void testCanIdentifySingleSaddlePoint() {
1818
Arrays.asList(6, 6, 7)
1919
));
2020

21-
List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(1, 0));
21+
Set<MatrixCoordinate> expectedSaddlePoints = Collections.singleton(new MatrixCoordinate(1, 0));
2222

2323
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
2424
}
@@ -28,7 +28,7 @@ public void testCanIdentifySingleSaddlePoint() {
2828
public void testCanIdentifyThatEmptyMatrixHasNoSaddlePoints() {
2929
Matrix matrix = new Matrix(new ArrayList<>());
3030

31-
List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
31+
Set<MatrixCoordinate> expectedSaddlePoints = Collections.emptySet();
3232

3333
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
3434
}
@@ -42,7 +42,7 @@ public void testCanIdentifyLackOfSaddlePointsWhenThereAreNone() {
4242
Arrays.asList(2, 3, 1)
4343
));
4444

45-
List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
45+
Set<MatrixCoordinate> expectedSaddlePoints = Collections.emptySet();
4646

4747
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
4848
}
@@ -56,11 +56,11 @@ public void testCanIdentifyMultipleSaddlePoints() {
5656
Arrays.asList(1, 5, 4)
5757
));
5858

59-
List<MatrixCoordinate> expectedSaddlePoints = Arrays.asList(
59+
Set<MatrixCoordinate> expectedSaddlePoints = new HashSet<>(Arrays.asList(
6060
new MatrixCoordinate(0, 1),
6161
new MatrixCoordinate(1, 1),
6262
new MatrixCoordinate(2, 1)
63-
);
63+
));
6464

6565
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
6666
}
@@ -74,7 +74,7 @@ public void testCanIdentifySaddlePointInBottomRightCorner() {
7474
Arrays.asList(3, 2, 5)
7575
));
7676

77-
List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(2, 2));
77+
Set<MatrixCoordinate> expectedSaddlePoints = Collections.singleton(new MatrixCoordinate(2, 2));
7878

7979
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
8080
}

0 commit comments

Comments
 (0)