Skip to content

Commit d1db138

Browse files
authored
refactor: revise obsolete solution logic (#852)
Return zero-size array with directions for empty input path.
1 parent 58744f3 commit d1db138

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

kata/6-kyu/shorter-path/main/ShortestPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ static String[] directions(String[] goal) {
88
var stats = of(goal).collect(groupingBy(identity(), counting()));
99
int y = (int) (stats.getOrDefault("N", 0L) - stats.getOrDefault("S", 0L));
1010
int x = (int) (stats.getOrDefault("E", 0L) - stats.getOrDefault("W", 0L));
11-
return (
11+
return x != 0 || y != 0 ? (
1212
"N".repeat(Math.max(0, y)) + "S".repeat(Math.max(0, -y)) +
1313
"E".repeat(Math.max(0, x)) + "W".repeat(Math.max(0, -x))
14-
).split("");
14+
).split("") : new String[0];
1515
}
1616
}

kata/6-kyu/shorter-path/test/DirectionsTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ class DirectionsTest {
77
@ParameterizedTest
88
@CsvSource(textBlock = """
99
NNNNNENN, NNNNNNNE
10+
SSNEWNWSE, S
1011
SSNEWSN, S
1112
NWSE,''
13+
N, N
14+
'', ''
1215
""")
1316
void sample(String path, String shortest) {
14-
assertArrayEquals(shortest.split(""), ShortestPath.directions(path.split("")));
17+
String[] goal = path.isEmpty() ? new String[0] : path.split("");
18+
String[] expected = shortest.isEmpty() ? new String[0] : shortest.split("");
19+
assertArrayEquals(expected, ShortestPath.directions(goal));
1520
}
1621
}

0 commit comments

Comments
 (0)