Skip to content

Commit e706dbb

Browse files
committed
saddle-points: add to track
1 parent ffbb48c commit e706dbb

8 files changed

Lines changed: 216 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@
324324
"slug": "book-store",
325325
"difficulty": 1,
326326
"topics": []
327+
},
328+
{
329+
"slug": "saddle-points",
330+
"difficulty": 1,
331+
"topics": []
327332
}
328333
],
329334
"deprecated": [
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
final class Matrix {
6+
7+
private final List<List<Integer>> values;
8+
9+
Matrix(final List<List<Integer>> values) {
10+
this.values = values;
11+
}
12+
13+
List<MatrixCoordinate> getSaddlePoints() {
14+
final List<MatrixCoordinate> result = new ArrayList<>();
15+
16+
if (values.isEmpty()) {
17+
return result;
18+
}
19+
20+
for (int row = 0; row < values.size(); row++) {
21+
for (int column = 0; column < values.get(0).size(); column++) {
22+
final int coordinateValue = values.get(row).get(column);
23+
24+
if (coordinateValue == getRowMax(row) && coordinateValue == getColumnMin(column)) {
25+
result.add(new MatrixCoordinate(row, column));
26+
}
27+
}
28+
}
29+
30+
return result;
31+
}
32+
33+
private int getRowMax(final int row) {
34+
return Collections.max(values.get(row));
35+
}
36+
37+
private int getColumnMin(final int column) {
38+
return values.stream()
39+
.map(row -> row.get(column))
40+
.min(Integer::compareTo)
41+
.get();
42+
}
43+
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
final class MatrixCoordinate {
2+
3+
private final int row;
4+
5+
private final int column;
6+
7+
MatrixCoordinate(final int row, final int column) {
8+
this.row = row;
9+
this.column = column;
10+
}
11+
12+
// Generated equals and hashcode.
13+
14+
@Override
15+
public boolean equals(final Object o) {
16+
if (this == o) return true;
17+
if (o == null || getClass() != o.getClass()) return false;
18+
19+
final MatrixCoordinate that = (MatrixCoordinate) o;
20+
21+
return row == that.row && column == that.column;
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
int result = row;
27+
result = 31 * result + column;
28+
return result;
29+
}
30+
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
final class Matrix {
2+
3+
4+
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
final class MatrixCoordinate {
2+
3+
private final int row;
4+
5+
private final int column;
6+
7+
MatrixCoordinate(final int row, final int column) {
8+
this.row = row;
9+
this.column = column;
10+
}
11+
12+
// Generated equals and hashcode.
13+
14+
@Override
15+
public boolean equals(final Object o) {
16+
if (this == o) return true;
17+
if (o == null || getClass() != o.getClass()) return false;
18+
19+
final MatrixCoordinate that = (MatrixCoordinate) o;
20+
21+
return row == that.row && column == that.column;
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
int result = row;
27+
result = 31 * result + column;
28+
return result;
29+
}
30+
31+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class MatrixTest {
12+
13+
@Test
14+
public void testCanIdentifySingleSaddlePoint() {
15+
Matrix matrix = new Matrix(Arrays.asList(
16+
Arrays.asList(9, 8, 7),
17+
Arrays.asList(5, 3, 2),
18+
Arrays.asList(6, 6, 7)
19+
));
20+
21+
List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(1, 0));
22+
23+
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
24+
}
25+
26+
@Ignore
27+
@Test
28+
public void testCanIdentifyThatEmptyMatrixHasNoSaddlePoints() {
29+
Matrix matrix = new Matrix(new ArrayList<>());
30+
31+
List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
32+
33+
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
34+
}
35+
36+
@Ignore
37+
@Test
38+
public void testCanIdentifyLackOfSaddlePointsWhenThereAreNone() {
39+
Matrix matrix = new Matrix(Arrays.asList(
40+
Arrays.asList(1, 2, 3),
41+
Arrays.asList(3, 1, 2),
42+
Arrays.asList(2, 3, 1)
43+
));
44+
45+
List<MatrixCoordinate> expectedSaddlePoints = new ArrayList<>();
46+
47+
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
48+
}
49+
50+
@Ignore
51+
@Test
52+
public void testCanIdentifyMultipleSaddlePoints() {
53+
Matrix matrix = new Matrix(Arrays.asList(
54+
Arrays.asList(4, 5, 4),
55+
Arrays.asList(3, 5, 5),
56+
Arrays.asList(1, 5, 4)
57+
));
58+
59+
List<MatrixCoordinate> expectedSaddlePoints = Arrays.asList(
60+
new MatrixCoordinate(0, 1),
61+
new MatrixCoordinate(1, 1),
62+
new MatrixCoordinate(2, 1)
63+
);
64+
65+
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
66+
}
67+
68+
@Ignore
69+
@Test
70+
public void testCanIdentifySaddlePointInBottomRightCorner() {
71+
Matrix matrix = new Matrix(Arrays.asList(
72+
Arrays.asList(8, 7, 9),
73+
Arrays.asList(6, 7, 6),
74+
Arrays.asList(3, 2, 5)
75+
));
76+
77+
List<MatrixCoordinate> expectedSaddlePoints = Collections.singletonList(new MatrixCoordinate(2, 2));
78+
79+
assertEquals(expectedSaddlePoints, matrix.getSaddlePoints());
80+
}
81+
82+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ include 'rna-transcription'
5151
include 'robot-name'
5252
include 'robot-simulator'
5353
include 'roman-numerals'
54+
include 'saddle-points'
5455
include 'scrabble-score'
5556
include 'secret-handshake'
5657
include 'series'

0 commit comments

Comments
 (0)