1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.*;
import java.util.stream.*;
public class SolutionTest {
@Test
public void testFixed() {
assertEquals("b***i***t***c***o***i***n", SortAndStar.twoSort(new String[] {"bitcoin", "take", "over", "the", "world", "maybe", "who", "knows", "perhaps"}));
assertEquals("a***r***e", SortAndStar.twoSort(new String[] {"turns", "out", "random", "test", "cases", "are", "easier", "than", "writing", "out", "basic", "ones"}));
assertEquals("a***b***o***u***t", SortAndStar.twoSort(new String[] {"lets", "talk", "about", "javascript", "the", "best", "language"}));
assertEquals("c***o***d***e", SortAndStar.twoSort(new String[] {"i", "want", "to", "travel", "the", "world", "writing", "code", "one", "day"}));
assertEquals("L***e***t***s", SortAndStar.twoSort(new String[] {"Lets", "all", "go", "on", "holiday", "somewhere", "very", "cold"}));
}
@Test
public void testRandom() {
Random random = new Random();
for(int i = 0; i < 200; i++){
int count = random.nextInt(100) + 1;
String[] test = new String[count];
for(int j = 0; j < count; j++) {
String testString = RandomStringUtils.randomAlphabetic(3, 10);
test[j] = testString;
}
assertEquals(SortAndStarSolution.twoSort(test), SortAndStar.twoSort(test));
}
}
}
class SortAndStarSolution {
public static String twoSort(String[] s) {
Arrays.sort(s);
return s[0].chars()
.mapToObj(value -> String.valueOf((char) value))
.collect(Collectors.joining("***"));
}
}
|