-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_validator.py
More file actions
50 lines (43 loc) · 1.43 KB
/
instance_validator.py
File metadata and controls
50 lines (43 loc) · 1.43 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
# Released to students
import sys
def main(argv):
if len(argv) != 1:
print "Usage: python instance_validator.py [path_to_input_file]"
return
print processInput(argv[0])
def processInput(s):
fin = open(s, "r")
line = fin.readline().split()
if len(line) != 1 or not line[0].isdigit():
return "Line 1 must contain a single integer."
N = int(line[0])
if N < 1 or N > 500:
return "N must be an integer between 1 and 500, inclusive."
line = fin.readline().split()
if len(line) > N:
return "There cannot be more than N children vertices."
children = []
for child in line:
if not child.isdigit():
return "The line of children must contain integers"
child = int(child)
if child < 0 or child >= N:
return "Each child index must be between 0 and N-1"
children.append(child)
d = [[0 for j in range(N)] for i in range(N)]
for i in xrange(N):
line = fin.readline().split()
if len(line) != N:
return "Line " + str(i+2) + " must contain N integers."
for j in xrange(N):
if not line[j].isdigit():
return "Line " + str(i+2) + " must contain N integers."
d[i][j] = int(line[j])
if d[i][j] < 0 or d[i][j] > 1:
return "The adjacency matrix must be comprised of 0s and 1s."
for i in xrange(N):
if d[i][i] != 0:
return "A node cannot have an edge to itself."
return "instance ok"
if __name__ == '__main__':
main(sys.argv[1:])