forked from louienicholaslee/MAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_validator.py
46 lines (41 loc) · 1.5 KB
/
solution_validator.py
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
import sys
instanceSizes = [4, 1, 7]
def main(argv):
if len(argv) != 1:
print "Usage: python solutions_validator.py [path_to_input_file]"
return
allPassed = True
lineIndex = 0
with open(argv[0], "r") as f:
for line in f:
if lineIndex >= len(instanceSizes):
print "Extra data at end of file"
return
result = processTest(line.split(), instanceSizes[lineIndex])
if result != "solution ok":
print "Error with test " + str(lineIndex + 1) + ": " + result
allPassed = False
lineIndex += 1
if lineIndex < len(instanceSizes):
print "File terminated early; missing lines"
return
if allPassed:
print "all solution lines ok"
def processTest(line, N):
if len(line) != N:
return "Line must contain " + str(N) + " integers."
b = [False for i in range(N)]
for v in line:
if not v.isdigit():
return "Line must contain " + str(N) + " integers."
vertex = int(v) - 1
if vertex < 0 or vertex >= N:
return "Each integer must be between 1 and " + str(N) + ", inclusive."
if b[vertex]:
return "Each integer in the range 1 to " + str(N) + " must appear exactly once."
b[vertex] = True
if not all(b):
return "Each integer in the range 1 to " + str(N) + " must appear exactly once."
return "solution ok"
if __name__ == '__main__':
main(sys.argv[1:])