update python code, remove __future__ imports, remove six, use absl-py for flags, update examples
This commit is contained in:
@@ -12,8 +12,6 @@
|
||||
# limitations under the License.
|
||||
"""This model implements a sudoku solver."""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
|
||||
@@ -28,10 +26,10 @@ def solve_sudoku():
|
||||
cell = list(range(0, cell_size))
|
||||
|
||||
initial_grid = [[0, 6, 0, 0, 5, 0, 0, 2, 0], [0, 0, 0, 3, 0, 0, 0, 9, 0],
|
||||
[7, 0, 0, 6, 0, 0, 0, 1, 0], [0, 0, 6, 0, 3, 0, 4, 0, 0], [
|
||||
0, 0, 4, 0, 7, 0, 1, 0, 0
|
||||
], [0, 0, 5, 0, 9, 0, 8, 0, 0], [0, 4, 0, 0, 0, 1, 0, 0, 6],
|
||||
[0, 3, 0, 0, 0, 8, 0, 0, 0], [0, 2, 0, 0, 4, 0, 0, 5, 0]]
|
||||
[7, 0, 0, 6, 0, 0, 0, 1, 0], [0, 0, 6, 0, 3, 0, 4, 0, 0],
|
||||
[0, 0, 4, 0, 7, 0, 1, 0, 0], [0, 0, 5, 0, 9, 0, 8, 0, 0],
|
||||
[0, 4, 0, 0, 0, 1, 0, 0, 6], [0, 3, 0, 0, 0, 8, 0, 0, 0],
|
||||
[0, 2, 0, 0, 4, 0, 0, 5, 0]]
|
||||
|
||||
grid = {}
|
||||
for i in line:
|
||||
@@ -68,14 +66,7 @@ def solve_sudoku():
|
||||
status = solver.Solve(model)
|
||||
if status == cp_model.OPTIMAL:
|
||||
for i in line:
|
||||
output = ''
|
||||
for j in line:
|
||||
output += str(int(solver.Value(grid[(i, j)]))) + ' '
|
||||
if j == 2 or j == 5:
|
||||
output += '| '
|
||||
print(output)
|
||||
if i == 2 or i == 5:
|
||||
print('------|-------|-------')
|
||||
print([int(solver.Value(grid[(i, j)])) for j in line])
|
||||
|
||||
|
||||
solve_sudoku()
|
||||
|
||||
Reference in New Issue
Block a user