forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
165 lines (139 loc) · 3.54 KB
/
Question.java
File metadata and controls
165 lines (139 loc) · 3.54 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Question17_5;
import java.util.Random;
public class Question {
public static class Result {
public int hits;
public int pseudoHits;
public Result(int h, int p) {
hits = h;
pseudoHits = p;
}
public Result() {
}
public String toString() {
return "(" + hits + ", " + pseudoHits + ")";
}
};
public static int code(char c) {
switch (c) {
case 'B':
return 0;
case 'G':
return 1;
case 'R':
return 2;
case 'Y':
return 3;
default:
return -1;
}
}
public static int MAX_COLORS = 4;
public static Result estimate(String guess, String solution) {
if (guess.length() != solution.length()) return null;
Result res = new Result();
int[] frequencies = new int[MAX_COLORS];
/* Compute hits and built frequency table */
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == solution.charAt(i)) {
res.hits++;
} else {
/* Only increment the frequency table (which will be used for pseudo-hits) if
* it's not a hit. If it's a hit, the slot has already been "used." */
int code = code(solution.charAt(i));
if (code >= 0) {
frequencies[code]++;
}
}
}
/* Compute pseudo-hits */
for (int i = 0; i < guess.length(); i++) {
int code = code(guess.charAt(i));
if (code >= 0 && frequencies[code] > 0 && guess.charAt(i) != solution.charAt(i)) {
res.pseudoHits++;
frequencies[code]--;
}
}
return res;
}
/************************** TEST CODE **********************************/
public static char letterFromCode(int k) {
switch (k) {
case 0:
return 'B';
case 1:
return 'G';
case 2:
return 'R';
case 3:
return 'Y';
default:
return '0';
}
}
public static Result estimateBad(String g, String s) {
char[] guess = g.toCharArray();
char[] solution = s.toCharArray();
int hits = 0;
for (int i = 0; i < guess.length; i++) {
if (guess[i] == solution[i]) {
hits++;
solution[i] = '0';
guess[i] = '0';
}
}
int pseudohits = 0;
for (int i = 0; i < guess.length; i++) {
if (guess[i] != '0') {
for (int j = 0; j < solution.length; j++) {
if (solution[j] != '0') {
if (solution[j] == guess[i]) {
pseudohits++;
solution[j] = '0';
break;
}
}
}
}
}
return new Result(hits, pseudohits);
}
public static String randomString() {
int length = 4;
char[] str = new char[length];
Random generator = new Random();
for (int i = 0; i < length; i++) {
int v = generator.nextInt(4);
char c = letterFromCode(v);
str[i] = c;
}
return String.valueOf(str);
}
public static boolean test(String guess, String solution) {
Result res1 = estimate(guess, solution);
Result res2 = estimateBad(guess, solution);
if (res1.hits == res2.hits && res1.pseudoHits == res2.pseudoHits) {
return true;
} else {
System.out.println("FAIL: (" + guess + ", " + solution + "): " + res1.toString() + " | " + res2.toString());
return false;
}
}
public static boolean testRandom() {
String guess = randomString();
String solution = randomString();
return test(guess, solution);
}
public static boolean test(int count) {
for (int i = 0; i < count; i++) {
if (!testRandom()) {
return true;
}
}
return false;
}
/********************** END TEST CODE ************************/
public static void main(String[] args) {
test(1000);
}
}