From e973a9f422afb1c4f12fc92039a140b94648834f Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Mon, 10 Mar 2025 13:14:15 -0400 Subject: [PATCH] feat: kata/sort-by-last-char --- kata/7-kyu/sort-by-last-char/README.md | 2 +- kata/7-kyu/sort-by-last-char/main/Kata.java | 8 ++++++ .../test/LastWordSortTest.java | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 kata/7-kyu/sort-by-last-char/main/Kata.java create mode 100644 kata/7-kyu/sort-by-last-char/test/LastWordSortTest.java diff --git a/kata/7-kyu/sort-by-last-char/README.md b/kata/7-kyu/sort-by-last-char/README.md index ca8a0089..dafdd73b 100644 --- a/kata/7-kyu/sort-by-last-char/README.md +++ b/kata/7-kyu/sort-by-last-char/README.md @@ -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. \ No newline at end of file diff --git a/kata/7-kyu/sort-by-last-char/main/Kata.java b/kata/7-kyu/sort-by-last-char/main/Kata.java new file mode 100644 index 00000000..60c37e1d --- /dev/null +++ b/kata/7-kyu/sort-by-last-char/main/Kata.java @@ -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); + } +} \ No newline at end of file diff --git a/kata/7-kyu/sort-by-last-char/test/LastWordSortTest.java b/kata/7-kyu/sort-by-last-char/test/LastWordSortTest.java new file mode 100644 index 00000000..470ce338 --- /dev/null +++ b/kata/7-kyu/sort-by-last-char/test/LastWordSortTest.java @@ -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 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)); + } +} \ No newline at end of file