-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting_tool.py
More file actions
133 lines (111 loc) · 3.2 KB
/
testing_tool.py
File metadata and controls
133 lines (111 loc) · 3.2 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
"""Golf Gophers interactive judge.
"""
# Usage: `python testing_tool.py test_number`, where the argument test_number is
# either 0 (first test set) or 1 (second test set).
# This can also be run as `python3 testing_tool.py test_number`.
from __future__ import print_function
import random
import sys
# Use raw_input in Python2.
try:
input = raw_input
except NameError:
pass
CASES = ([1, 2, 3],
[1, 2, 3]) # fill in your own cases
QS = (365, 7)
MAX_GOPHERS = (100, 10 ** 6)
WRONG_ANSWER, CORRECT_ANSWER = -1, 1
EXCEEDED_QUERIES_ERROR = "Exceeded number of queries: {}.".format
INVALID_LINE_ERROR = "Couldn't read a valid line."
NOT_INTEGER_ERROR = "Not an integer: {}".format
NUM_BLADES_OUT_OF_RANGE_ERROR = "Num blades {} is out of range [2-18].".format
NUM_GOPHERS_OUT_OF_RANGE_ERROR = "Num gophers {} is out of range [1-{}].".format
WRONG_NUM_TOKENS_ERROR = "Wrong number of tokens: {}. Expected 1 or 18.".format
WRONG_GUESS_ERROR = "Wrong guess: {}. Expected: {}.".format
def ReadValues(line, mg=None):
t = line.split()
if len(t) != 1 and len(t) != 18:
return WRONG_NUM_TOKENS_ERROR(len(t))
r = []
for s in t:
try:
v = int(s)
except:
return NOT_INTEGER_ERROR(s if len(s) < 100 else s[:100])
r.append(v)
if len(r) == 1:
if not (1 <= r[0] <= mg):
return NUM_GOPHERS_OUT_OF_RANGE_ERROR(r[0], mg)
else:
for ri in r:
if not (2 <= ri <= 18):
return NUM_BLADES_OUT_OF_RANGE_ERROR(ri)
return r
def GopherChoices(g):
r = [0] * 18
for _ in range(g):
r[random.randrange(18)] += 1
return r
def RunCase(qs, mg, case, test_input=None):
outputs = []
def Input():
return input()
def Output(line):
print(line)
sys.stdout.flush()
for ex in range(qs + 1):
try:
line = Input()
except:
Output(WRONG_ANSWER)
return INVALID_LINE_ERROR, outputs
v = ReadValues(line, mg)
if isinstance(v, str):
Output(WRONG_ANSWER)
return v, outputs
if len(v) == 18:
if ex == qs:
Output(WRONG_ANSWER)
return EXCEEDED_QUERIES_ERROR(qs), outputs
else:
r = GopherChoices(case)
for i in range(18):
r[i] %= v[i]
Output(" ".join(map(str, r)))
else:
if v[0] != case:
Output(WRONG_ANSWER)
return WRONG_GUESS_ERROR(v[0], case), outputs
else:
Output(CORRECT_ANSWER)
return None, outputs
def RunCases(qs, mg, cases):
for i, case in enumerate(cases, 1):
result, _ = RunCase(qs, mg, case)
if result:
return "Case #{} ({}) failed: {}".format(i, case, result)
try:
extra_input = input()
except EOFError:
return None
except Exception: # pylint: disable=broad-except
return "Exception raised while reading input after all cases finish."
return "Additional input after all cases finish: {}".format(extra_input[:100])
def main():
random.seed(2)
assert len(sys.argv) == 2
index = int(sys.argv[1])
cases = CASES[index]
qs = QS[index]
mg = MAX_GOPHERS[index]
random.shuffle(cases)
print(len(cases), qs, mg)
sys.stdout.flush()
result = RunCases(qs, mg, cases)
if result:
print(result, file=sys.stderr)
sys.stdout.flush()
sys.exit(1)
if __name__ == "__main__":
main()