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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;
public class SolutionTest{
private static Random random = new Random();
private static int asg(String s){
int res = 0, limit = Math.min(8,s.length()/2);
for (int c = 1; c < limit; ++c) {
int temp = 1;
int m = Integer.parseInt(s.substring(0, c));
int hold = c;
while (hold < s.length()) {
m++;
String n = String.valueOf(m);
if (!n.equals(s.substring(hold, hold+n.length()))) {
res = m;
temp--;
if (temp < 0) break;
}
else hold += n.length();
}
if (temp == 0 && hold == s.length()) return (int)res;
}
return -1;
}
private static int random(int l, int u){
return random.nextInt(u-l)+l;
}
@Test
public void basicTests(){
assertEquals(4,Solution.missing("123567"));
assertEquals(92,Solution.missing("899091939495"));
assertEquals(100,Solution.missing("9899101102"));
assertEquals(-1,Solution.missing("599600601602"));
assertEquals(-1,Solution.missing("8990919395"));
assertEquals(1002,Solution.missing("998999100010011003"));
assertEquals(10000,Solution.missing("99991000110002"));
assertEquals(-1,Solution.missing("979899100101102"));
assertEquals(900003,Solution.missing("900001900002900004900005900006"));
}
@Test
public void randomTests(){
for (int i = 0; i < 100; i++) {
int len = random(8,12);
ArrayList <Integer> arr1 = new ArrayList<>();
ArrayList <Integer> arr2 = new ArrayList<>();
int seq = random(1,975000);
arr1.add(seq);
int edge = random(2,5);
int num = (int)Math.pow(10, edge);
int seq2 = num - len + 2;
arr2.add(seq2);
for (int j = 0; j <= len; j++) {
seq++;
seq2++;
arr1.add(seq);
arr2.add(seq2);
}
int trap = random(0,20);
String test1 = "", test2 = "";
int exp1, exp2;
if (trap > 7) {
arr1.remove(random(1,arr1.size()-1));
arr2.remove(random(1,arr2.size()-1));
int checker = random(0,20);
if (checker < 5) {
if (arr1.get(1) % 2 == 0)
arr1.remove(random(1,arr1.size()-1));
else
arr2.remove(random(1,arr2.size()-1));
}
for (int e : arr1)
test1 += String.valueOf(e);
for (int e : arr2)
test2 += String.valueOf(e);
exp1 = asg(test1);
exp2 = asg(test2);
assertEquals(exp1,Solution.missing(test1));
assertEquals(exp2,Solution.missing(test2));
} else {
for (int e : arr1)
test1 += String.valueOf(e);
for (int e : arr2)
test2 += String.valueOf(e);
exp1 = asg(test1);
exp2 = asg(test2);
assertEquals(exp1,Solution.missing(test1));
assertEquals(exp2,Solution.missing(test2));
}
}
}
}
|