# Python - Sudoku checker

Page 1 of 1

## 2 Replies - 11808 Views - Last Post: 19 May 2010 - 08:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=170781&amp;s=212745e6560d7b19982d1c831e924b49&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chavana23

Reputation: 0
• Posts: 10
• Joined: 17-September 09

# Python - Sudoku checker

Posted 28 April 2010 - 06:58 AM

i have to write a program that checks the validity of a 9 x 9 sudoku 'matrix'. It must check rows, columns and 3 x 3 blocks and output valid if so, i have managed to check the rows bt ineed help on how to check the columns and 3 x 3 blocks.the digits have spaces in between and input terminates with a blank line

this is how i check, please fill free to comment
```x = raw_input()

count = 0
ans = 1

while x != '':
list2 = x.split(' ')

for j in range (0,9):
if list2.count(list2[j]) <= 1:
count = count + 1
else:
count = count + 50

x = raw_input()

if count <= 49:
print 'valid'
else:
print 'invalid'

```

Is This A Good Question/Topic? 0

## Replies To: Python - Sudoku checker

### #2 chavana23

Reputation: 0
• Posts: 10
• Joined: 17-September 09

## Re: Python - Sudoku checker

Posted 19 May 2010 - 05:22 AM

```def check(ls):
count = 0
for j in range (0,9):
for n in range(0,9):
if ls[j].count(ls[j][n]) <= 1:
count = count + 0
else:
count = count + 1

cols = [[row[i] for row in ls] for i in[0,1,2,3,4,5,6,7,8]]
leg = 0
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) <= 1:
leg = leg + 0
else:
leg = leg + 1
angel = []
for t in range(3):
ang = ls[t]
for u in range(3):
angel.append(ang[u])
foot = 0
for be in range(9):
if angel.count(angel[be]) <= 1:
foot = foot + 0
else:
foot = foot + 1

if count + leg + foot == 0:
return "Valid"
else:
return "Invalid"

def inputs():
x = raw_input()
ls = []
while x != '':
x1 =x.split(' ')
ls.append(x1)
if len(ls) >=9:
print check(ls)
ls = []
x = raw_input()
inputs()

```

### #3 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: Python - Sudoku checker

Posted 19 May 2010 - 08:15 AM

Is there a question?

It seems valid, if a little messy. You don't need all the counters or + 0:
```def check(ls):
for j in range (9):
for n in range(9):
if ls[j].count(ls[j][n]) > 1:
return False

cols = [[row[i] for row in ls] for i in range(9)]
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) > 1:
return False

angel = []
for t in range(3):
ang = ls[t]
for u in range(3):
angel.append(ang[u])

for be in range(9):
if angel.count(angel[be]) > 1:
return False

return True

```

I'd probably do it like this:
```def boardCheck(board):
def box(row,col):
return ((row / 3)*3) + (col / 3)

chk = [[[False for i in range(9)] for i in range(9)] for i in range(3)]
for row in range(9):
for col in range(9):
n = int(board[row][col]) - 1
if n in range(1,10):
loc = (row, col, box(row,col))
for i in range(3):
chkList = chk[i][loc[i]]
if chkList[n]:
return False
else:
chkList[n] = True
return True

```

This post has been edited by baavgai: 19 May 2010 - 08:15 AM