-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac785e0
commit c928151
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/test/java/io/github/sandeeplakka/codewars/kyu8/SortAndStar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.sandeeplakka.codewars.kyu8; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/* | ||
Sort and Star | ||
You will be given a list of strings. You must sort it alphabetically | ||
(case-sensitive, and based on the ASCII values of the chars) and then return the first value. | ||
The returned value must be a string, and have "***" between each of its letters. | ||
You should not remove or add elements from/to the array. | ||
Categories : Fundamentals, Strings, Arrays | ||
*/ | ||
public class SortAndStar { | ||
|
||
@Test | ||
public void testFixed() { | ||
assertEquals("b***i***t***c***o***i***n", | ||
twoSort(new String[]{"bitcoin", "take", "over", "the", "world", "maybe", "who", "knows", "perhaps"})); | ||
assertEquals("a***r***e", | ||
twoSort(new String[]{"turns", "out", "random", "test", "cases", "are", "easier", | ||
"than", "writing", "out", "basic", "ones"})); | ||
assertEquals("a***b***o***u***t", | ||
twoSort(new String[]{"lets", "talk", "about", "javascript", "the", "best", "language"})); | ||
assertEquals("c***o***d***e", | ||
twoSort(new String[]{"i", "want", "to", "travel", "the", "world", "writing", "code", "one", "day"})); | ||
assertEquals("L***e***t***s", | ||
twoSort(new String[]{"Lets", "all", "go", "on", "holiday", "somewhere", "very", "cold"})); | ||
} | ||
|
||
public static String twoSort(String[] s) { | ||
if (s.length == 0) return null; | ||
|
||
String result = s[0]; | ||
for (String value : s) { | ||
if (value.charAt(0) < result.charAt(0)) { | ||
result = value; | ||
} | ||
} | ||
return Arrays.stream(result.split("")).collect(Collectors.joining("***")); | ||
} | ||
} |