Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kata/sort-by-last-char #794

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@
- [Sort an array by value and index](sort-an-array-by-value-and-index "58e0cb3634a3027180000040")
- [Sort arrays - 1](sort-arrays-1 "51f41b98e8f176e70d0002a8")
- [Sort by binary ones](sort-by-binary-ones "59eb28fb0a2bffafbb0000d6")
- [Sort by Last Char](sort-by-last-char "57eba158e8ca2c8aba0002a0")
- [Sort deck of cards](sort-deck-of-cards "56f399b59821793533000683")
- [Sort Out The Men From Boys](sort-out-the-men-from-boys-1 "5af15a37de4c7f223e00012d")
- [Sort the Gift Code](sort-the-gift-code "52aeb2f3ad0e952f560005d3")
Expand Down
7 changes: 7 additions & 0 deletions kata/7-kyu/sort-by-last-char/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# [Sort by Last Char](https://www.codewars.com/kata/sort-by-last-char "https://www.codewars.com/kata/57eba158e8ca2c8aba0002a0")

Given a string of words (x), you need to return an array of the words, sorted alphabetically by the final character in each.

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.
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));
}
}
Loading