68. Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudio
v$(VisualStudioVersion)Python
ToolsMicrosoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build
command in
Visual Studio and specify your pre- and post-build
commands in
the BeforeBuild and AfterBuild targets below. -->
<!--<Target Name="CoreCompile" />-->
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
</Project>
template/PythonApplication1/sudoku.py
import copy
def is_number_in_row(board, row, number):
for j in range(9):
if board[row][j] == number:
69. return True
return False
def is_number_in_column(board, column, number):
for i in range(9):
if board[i][column] == number:
return True
return False
def is_number_in_subgrid(board, subgrid_i, subgrid_j, number):
for i in range(3):
for j in range(3):
if board[3*subgrid_i + i][3*subgrid_j + j] == number:
return True
return False
70. def solve_sudoku(input_board):
def solve(board):
for i in range(9):
for j in range(9):
# the cell was already solved
if board[i][j] != 0:
continue
for number in range(1, 10):
if not is_number_in_row(board, i, number) and not
is_number_in_column(board, j, number) and not
is_number_in_subgrid(board, i//3, j//3, number):
board[i][j] = number
solution = solve(board)
if solution != None:
return solution
board[i][j] = 0
71. # backtrack
return None
# TODO: check if the input grid is valid
return board
# use deepcopy so the solver does not modify the input board
return solve(copy.deepcopy(input_board))
template/PythonApplication1/template.py
import time
import module # imports the C++ extension
from sudoku import solve_sudoku
start_time = time.perf_counter()
# put here the function you want to time
result = module.example(10)
72. elapsed_time = time.perf_counter() - start_time
print(f'Result: {result}')
print(f'Execution time: {1000*elapsed_time:.4f} ms')
template/template.sln
Microsoft Visual Studio
Solution
File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") =
"PythonExtension", "ExtensionExtension.vcxproj",
"{EFCC7D22-F00E-4D12-A31E-F7B66231AB06}"
EndProject