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
45
46
47
48
|
import java.util.Random;
import java.util.TreeSet;
import static Solution.capitalize;
import org.junit.runners.JUnit4;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SolutionTest{
private static final String lowercase = "abcdefghijklmnopqrstuvwxyz";
private static final Random random = new Random();
@Test
public void basicTest(){
assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5}));
assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5,100}));
assertEquals("abRacaDabRA", capitalize("abracadabra", new int[]{2,6,9,10}));
assertEquals("Indexinglessons", capitalize("indexinglessons", new int[]{0}));
}
private String solve_kvWOF(String s, int[] ind){
StringBuilder sb = new StringBuilder(s);
for(int i : ind){
if(i < sb.length())
sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
}
return sb.toString();
}
@Test
public void randomTest(){
for(int i = 0; i < 100; i++){
int r = random.nextInt(10) + 10;
StringBuilder sb = new StringBuilder();
for(int j = 0; j < r; j++)
sb.append(lowercase.charAt(random.nextInt(lowercase.length())));
TreeSet<Integer> set = new TreeSet<Integer>();
for(int j = 0; j < sb.length(); j++)
set.add(random.nextInt(r));
int[] a = new int[set.size()];
for(int j = 0; j < a.length; j++)
a[j] = set.pollFirst();
String str = sb.toString();
assertEquals(solve_kvWOF(str, a), capitalize(str, a));
}
}
}
|