Skip to content

Commit e973a9f

Browse files
committed
1 parent 74028d7 commit e973a9f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

kata/7-kyu/sort-by-last-char/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Given a string of words (x), you need to return an array of the words, sorted al
44

55
If two words have the same last letter, the returned array should show them in the order they appeared in the given string.
66

7-
All inputs will be valid.
7+
All inputs will be valid.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import static java.util.Comparator.comparingInt;
2+
import static java.util.stream.Stream.of;
3+
4+
interface Kata {
5+
static String[] last(String x) {
6+
return of(x.split(" ")).sorted(comparingInt(s -> s.charAt(s.length() - 1))).toArray(String[]::new);
7+
}
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2+
import static org.junit.jupiter.params.provider.Arguments.arguments;
3+
4+
import java.util.stream.Stream;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
class LastWordSortTest {
10+
private static Stream<Arguments> testData() {
11+
return Stream.of(
12+
arguments("man i need a taxi up to ubud", new String[]{"a", "need", "ubud", "i", "taxi", "man", "to", "up"}),
13+
arguments("what time are we climbing up the volcano", new String[]{"time", "are", "we", "the", "climbing", "volcano", "up", "what"}),
14+
arguments("take me to semynak", new String[]{"take", "me", "semynak", "to"}),
15+
arguments("massage yes massage yes massage", new String[]{"massage", "massage", "massage", "yes", "yes"}),
16+
arguments("take bintang and a dance please", new String[]{"a", "and", "take", "dance", "please", "bintang"})
17+
);
18+
}
19+
20+
@ParameterizedTest
21+
@MethodSource("testData")
22+
void sample(String s, String[] expected) {
23+
assertArrayEquals(expected, Kata.last(s));
24+
}
25+
}

0 commit comments

Comments
 (0)