Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ParanoidUser committed Mar 10, 2025
1 parent 74028d7 commit e973a9f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kata/7-kyu/sort-by-last-char/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Given a string of words (x), you need to return an array of the words, sorted al

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

All inputs will be valid.
All inputs will be valid.
8 changes: 8 additions & 0 deletions kata/7-kyu/sort-by-last-char/main/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import static java.util.Comparator.comparingInt;
import static java.util.stream.Stream.of;

interface Kata {
static String[] last(String x) {
return of(x.split(" ")).sorted(comparingInt(s -> s.charAt(s.length() - 1))).toArray(String[]::new);
}
}
25 changes: 25 additions & 0 deletions kata/7-kyu/sort-by-last-char/test/LastWordSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class LastWordSortTest {
private static Stream<Arguments> testData() {
return Stream.of(
arguments("man i need a taxi up to ubud", new String[]{"a", "need", "ubud", "i", "taxi", "man", "to", "up"}),
arguments("what time are we climbing up the volcano", new String[]{"time", "are", "we", "the", "climbing", "volcano", "up", "what"}),
arguments("take me to semynak", new String[]{"take", "me", "semynak", "to"}),
arguments("massage yes massage yes massage", new String[]{"massage", "massage", "massage", "yes", "yes"}),
arguments("take bintang and a dance please", new String[]{"a", "and", "take", "dance", "please", "bintang"})
);
}

@ParameterizedTest
@MethodSource("testData")
void sample(String s, String[] expected) {
assertArrayEquals(expected, Kata.last(s));
}
}

0 comments on commit e973a9f

Please sign in to comment.