Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import csv import itertools class Board ( ) : ########################################## # Constructor ########################################## def _ _ init _ _ ( self , filename ) :
import csv
import itertools
class Board:
##########################################
# Constructor
##########################################
def initself filename:
self.n
self.n
self.spaces
self.board None
self.valsInRows None
self.valsInCols None
self.valsInBoxes None
self.unsolvedSpaces None
self.loadSudokufilename
def loadSudokuself filename:
with openfilename as csvFile:
self.n
reader csvreadercsvFile
for row in reader:
if self.n :
self.n intlenrow
if not self.n lenrow:
raise ExceptionEach row must have n values! See row
else:
self.n lenrow
self.spaces self.n
self.board
self.valsInRows set for in rangeselfn
self.valsInCols set for in rangeselfn
self.valsInBoxes set for in rangeselfn
self.unsolvedSpaces setitertoolsproductrangeselfn rangeselfn
else:
if lenrow self.n:
raise ExceptionEach row must have the same number of values. See row strreaderlinenum
for index, item in enumeraterow:
if not item :
self.boardreaderlinenum index intitem
self.valsInRowsreaderlinenumaddintitem
self.valsInColsindexaddintitem
self.valsInBoxesselfspaceToBoxreaderlinenum indexaddintitem
self.unsolvedSpaces.removereaderlinenum index
##########################################
# Utility Functions
##########################################
def spaceToBoxself row, col:
return self.n row self.n col self.n
def printself:
for r in rangeselfn:
if r self.n and not r :
if self.n:
print self.n
else:
print self.n
row
for c in rangeselfn:
if rc in self.board:
val self.boardrc
else:
val None
if c self.n and not c :
row
else:
row
if self.n:
if val is None: row
else: row i val
else:
if val is None: row
else: row strval
printrow
##########################################
# Move Functions YOUR IMPLEMENTATIONS GO HERE
##########################################
# makes a move, records it in its row, col, and box, and removes the space from unsolvedSpaces
def makeMoveself space, value:
raise NotImplementedError
# removes the move, its record in its row, col, and box, and adds the space back to unsolvedSpaces
def undoMoveself space, value:
raise NotImplementedError
# returns True if the space is empty and on the board,
# and assigning value to it if not blocked by any constraints
def isValidMoveself space, value:
raise NotImplementedError
# optional helper function for use by getMostConstrainedUnsolvedSpace
def evaluateSpaceself space:
raise NotImplementedError
# gets the unsolved space with the most current constraints
# returns None if unsolvedSpaces is empty
def getMostConstrainedUnsolvedSpaceself:
raise NotImplementedError
class Solver:
##########################################
#### Constructor
##########################################
def initself:
pass
####################################
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started