-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
59 lines (51 loc) · 1.64 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
def main(argv):
if len(argv) != 1:
print "Usage: python validator.py [path_to_input_file]"
return
print processFile(argv[0])
def processFile(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 < 4 or N > 50 or N % 2 != 0:
return "N must be an even integer between 4 and 50, inclusive."
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 " + `i+2` + " must contain N integers."
for j in xrange(N):
if not line[j].isdigit():
return "Line " + `i+2` + " must contain N integers."
d[i][j] = int(line[j])
a = int(line[j])
if d[i][j] < 0 or d[i][j] > 100:
return "All edge weights must be between 0 and 100, inclusive."
for i in xrange(N):
if d[i][i] != 0:
return "The distance from a node to itself must be 0."
for j in xrange(N):
if d[i][j] != d[j][i]:
return "The distance matrix must be symmetric."
line = fin.readline().strip()
if len(line) != N:
return "Line " + `N+2` + " must be a string of length N."
r = 0
b = 0
for j in xrange(N):
c = line[j]
if c != 'R' and c != 'B':
return "Each character of the string must be either R or B."
if c == 'R': r += 1
if c == 'B': b += 1
if r != b:
return "The number of red and blue cities must be equal."
line = fin.readline()
if len(line) != 0:
return "The file must have exactly N+2 lines."
return "ok"
if __name__ == '__main__':
main(sys.argv[1:])