Sudoku in python

vickybat

I am the night...I am...
Guys, please see if the logic of sudoku checker is right or not in my code

Here's a bit of info about a sudoku checker

Sudoku [*en.wikipedia.org/wiki/Sudoku]
is a logic puzzle where a game
is defined by a partially filled
9 x 9 square of digits where each square
contains one of the digits 1,2,3,4,5,6,7,8,9.
For this question we will generalize
and simplify the game.

Define a procedure, check_sudoku,
that takes as input a square list
of lists representing an n x n
sudoku puzzle solution and returns the boolean
True if the input is a valid
sudoku square and returns the boolean False
otherwise.

A valid sudoku square satisfies these
two properties:

1. Each column of the square contains
each of the whole numbers from 1 to n exactly once.

2. Each row of the square contains each
of the whole numbers from 1 to n exactly once.

You may assume the the input is square and contains at
least one row and column.

Here's my code in python:

PHP:
correct = [[1,2,3],
           [2,3,1],
           [3,1,2]]

incorrect = [[1,2,3,4],
             [2,3,1,3],
             [3,1,2,3],
             [4,4,4,4]]

incorrect2 = [[1,2,3,4],
             [2,3,1,4],
             [4,1,2,3],
             [3,4,1,2]]

incorrect3 = [[1,2,3,4,5],
              [2,3,1,5,6],
              [4,5,2,1,3],
              [3,4,5,2,1],
              [5,6,4,3,2]]

incorrect4 = [['a','b','c'],
              ['b','c','a'],
              ['c','a','b']]

incorrect5 = [ [1, 1.5],
               [1.5, 1]]
               

def check_sudoku(p):
    s =p[0] # top most list object using list index
    n =len(p) # length of list
    for e in s: # checking each element in list
        i=0
        
        while i < n: # outer loop for rows and columns 
            j=0
            row = 0
            col = 0
            while j < n: #inner loop for rows and columns
                if p[i][j]== e:  # traversing list by rows and comparing with the digit
                   row = row + 1
                if p[j][i] == e: # traversing list by columns and comparing with digit
                   col = col + 1
                  
                j = j + 1
           
            if isinstance (e,str) or not isinstance(e,int)or e == 0 : # condition to check that each element should not be a character,non-integer or zero
                   return False
                
            if row!=1 or col != 1: # if each element count exceeds 1, then it returns false i.e not sudoku
         
                return False

            i = i + 1
    return True  # Its a sudoku
    
       
                 



    
    
print check_sudoku(incorrect)
#>>> False

print check_sudoku(correct)
#>>> True

print check_sudoku(incorrect2)
#>>> False

print check_sudoku(incorrect3)
#>>> False

print check_sudoku(incorrect4)
#>>> False

print check_sudoku(incorrect5)
#>>> False


Be careful about the indentations as python is strict about whitespaces.
 
Last edited:

skeletor

Chosen of the Omnissiah
what does it do? checker as in solver?? :confused:

Sudoku has one more condition. Each mini square also contains no repeated number.
 
OP
vickybat

vickybat

I am the night...I am...
what does it do? checker as in solver?? :confused:

Sudoku has one more condition. Each mini square also contains no repeated number.

Its a checker and not a solver. Yes i'm aware of that condition buddy.
Just wanted to be sure if it works the right way or not. Any help is appreciated. :)
 
Top Bottom