-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_io.py
133 lines (93 loc) · 3.55 KB
/
sudoku_io.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
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
from sudoku_solver import *
def parse_sudoku(input_filename):
# n: number of nodes in line/column
# sep: separator between node values [spc: single space, tab: tab]
# unknown_value: char used for marking unknown node value
n = None
myrange = None
sep = None
unknown_value = None
parameter_mode = False
sudoku_parsing_mode = False
sudoku = None
# The things get a bit uglier here.
try:
srow = 0 # sudoku row
for line in open(input_filename, 'r'):
if line[:2] == "?P":
parameter_mode = True
elif line[:2] == "?S":
parameter_mode = False
sudoku_parsing_mode = True
if myrange is None:
myrange = [str(x) for x in range(0, n)]
sep = " "
unknown_value = "*"
elif sep is None:
sep = " "
unknown_value = "*"
elif unknown_value is None:
unknown_value = "*"
sudoku = Sudoku(n, myrange)
elif line[:2] == "?E":
parameter_mode = False
sudoku_parsing_mode = False
return sudoku
elif parameter_mode:
data = line.split()
if n is None:
n = int(data[0])
elif myrange is None:
try:
ci = data.index("#") # comment index
myrange = data[:ci]
except ValueError:
myrange = data
elif sep is None:
if data[0] == "spc":
sep = " "
elif data[0] == "tab":
sep = "\t"
else:
sep = data[0]
elif unknown_value is None:
if data[0] == "spc":
unknown_value = " "
elif data[0] == "tab":
unknown_value = "\t"
else:
unknown_value = data[0]
elif sudoku_parsing_mode:
values = line.split(sep=sep)
for scol in range(0, n):
value = values[scol].strip()
if value in myrange:
sudoku.set_value(srow, scol, value)
elif value is unknown_value:
pass
else:
raise ValueError
srow += 1
return sudoku
except ValueError:
print("Invalid input file formatting.")
except FaultySolution:
print("Sudoku in input file is not valid.")
def write_solution_to_file(solutions, output_filename, sep=" "):
with open(output_filename, "w") as file:
if len(solutions) == 0:
file.write("No solution was found.")
elif len(solutions) == 1:
solution = solutions[0]
sol_keys = [x for (x, y) in list(solution.keys())]
n = max(sol_keys) + 1
for i in range(0, n):
line = ""
for j in range(0, n):
line += solution[(i, j)]
line += sep
line += "\n"
file.write(line)
file.close()
else:
file.write("This feature is not implemented yet and apparently it works by itself, great.")