SUDOKU validation

Given an incomplete Sudoku configuration in terms of a 9x9 2-D square matrix (mat[][]) the task to check if the configuration has a solution or not.



class SudokuValidator(object):
    def __init__(self,arr):
        self.mat = [[0]*9 for i in range(9)]
        index = 0
        while index<len(arr):
            self.mat[index/9][index%9] = arr[index]
            index+=1
    def show(self):
        pprint(self.mat)

    def validate(self):
        #checking rows
        for i in range(0,9):
            if not self.validate_row_col(i,i+1,0,9):
                return False
        print "rows are fine"   
        #checking cols
        for i in range(0,9):
            if not self.validate_row_col(0,9,i,i+1):
                return False
        print "cols are fine"
        #checking grid
        i=0
        while i<=6:
            j=0
            while j<=6:
                if not self.validate_row_col(i,i+3,j,j+3):
                    return False
                j+=3
            i+=3

        return True
    def validate_row_col(self,row_i,row_j,col_i,col_j):
        int_flag = [False]*10
        for i in range(row_i,row_j):
            for j in range(col_i,col_j):
                if (self.mat[i][j]!=0 and int_flag[self.mat[i][j]]):
                    print row_i,row_j,col_i,col_j
                    return False
                else:
                    int_flag[self.mat[i][j]] = True
        return True


results matching ""

    No results matching ""