# 2 by 2 sudoku algorithm (python)

Watch
Announcements
#1
hello,

I am doing a 2 by 2 Sudoku algorithm on python. This is what I have so far.

import random

gridOne = [1,0,4,0]
gridTwo = [0,3,0,0]
gridThree = [0,2,0,1]
gridFour = [4,1,0,0]

for i in gridOne:
while [i] == (0):
[i] == random.randint (1,4) # change 0 with a random (1,4) number
while [i] == [i]:
[i] == random.randint (1,4)

for i in gridTwo:
while [i] == (0):
[i] == random.randint (1,4)
while [i] == [i]:
[i] == random.randint (1,4)

for i in gridThree:
while [i] == (0):
[i] == random.randint (1,4)
while [i] == [i]:
[i] == random.randint (1,4)

for i in gridFour:
while [i] == (0):
[i] == random.randint (1,4)
while [i] == [i]:
[i] == random.randint (1,4)

for i in range (1,4):
while  or  in (gridOne) ==  or  in (gridTwo): # if top row in grid 1 and 2 has a repeated number
 in (gridOne) == random.randint (1,4) # change the duplicated number
 in (gridTwo) == random.randint (1,4)
while [0,1] in (gridOne) and [0,1] in (gridTwo) != (1,4): # if row isn't 1-4 then change the numbers that aren't fixed
 in (gridOne) == random.randint (1,4)
 in (gridTwo) == random.randint (1,4)

while  or  in (gridOne) ==  or  in (gridTwo): # if bottom row in grid 1 and 2 has a repeated number
 in (gridOne) == random.randint (1,4) # change the duplicated number
 in (gridTwo) == random.randint (1,4)
 in (gridTwo) == random.randint (1,4)
while [2,3] in (gridOne) and [2,3] in (gridTwo) != (1,4): # if row isn't 1-4 then change the numbers that aren't fixed
 in (gridOne) == random.randint (1,4)
 in (gridTwo) == random.randint (1,4)
 in (gridTwo) == random.randint (1,4)

while  or  in (gridThree) ==  or  in (gridFour): # if top row in grid 3 and 4 has a repeated number
 in (gridThree) == random.randint (1,4) # change the duplicated number
while [0,1] in (gridOne) and [0,1] in (gridTwo) != (1,4): # if row isn't 1-4 then change the numbers that aren't fixed
 in (gridThree) == random.randint (1,4)

while  or  in (gridThree) ==  or  in (gridFour): # if bottom row in grid 3 and 4 has a repeated number
 in (gridThree) == random.randint (1,4) # change the duplicated number
 in (gridFour) == random.randint (1,4)
 in (gridFour) == random.randint (1,4)
while [2,3] in (gridThree) and [2,3] in (gridFour) != (1,4): # if row isn't 1-4 then change the numbers that aren't fixed
 in (gridThree) == random.randint (1,4)
 in (gridFour) == random.randint (1,4)
 in (gridFour) == random.randint (1,4)

while  or  in (gridOne) ==  or  in (gridThree): # if first column has a repeated number
 in (gridThree) == random.randint (1,4) # change the duplicated number
 in (gridThree) == random.randint (1,4)
while [0,2] in (gridOne) and [0,2] in (gridThree) != (1,4): # if column isn't 1-4 then change the numbers that aren't fixed
 in (gridThree) == random.randint (1,4)
 in (gridThree) == random.randint (1,4)

while  or  in (gridOne) ==  or  in (gridThree): # if second column has a repeated number
 in (gridOne) == random.randint (1,4) # change the duplicated number
 in (gridOne) == random.randint (1,4)
while [1,3] in (gridOne) and [1,3] in (gridThree) != (1,4): # if column isn't 1-4 then change the numbers that aren't fixed
 in (gridOne) == random.randint (1,4)
 in (gridOne) == random.randint (1,4)

while  or  in (gridTwo) ==  or  in (gridFour): # if third column has a repeated number
 in (gridtwo) == random.randint (1,4) # change the duplicated number
 in (gridTwo) == random.randint (1,4)
 in (gridFour) == random.randint (1,4)
while [0,2] in (gridTwo) and [0,2] in (gridFour) != (1,4): # if column isn't 1-4 then change the numbers that aren't fixed
 in (gridtwo) == random.randint (1,4)
 in (gridTwo) == random.randint (1,4)
 in (gridFour) == random.randint (1,4)

while  or  in (gridTwo) ==  or  in (gridFour): # if fourth column has a repeated number
 in (gridTwo) = random.randint (1,4) # change the duplicated number
 in (gridFour) = random.randint (1,4)
while [1,3] in (gridTwo) and [1,3] in (gridFour) != (1,4): # if column isn't 1-4 then change the numbers that aren't fixed
 in (gridTwo) = random.randint (1,4)
 in (gridFour) = random.randint (1,4)

It's not fully completed and I know there is a much simpler way but I can't figure out what that is. Any help would be much appreciated.

Thanks!
0
3 years ago
#2
Its not clear what you are trying to do ?
Solve Sudokus ?
Validate winning Sudokus ?
Generate them ?

I'm guessing by the code it is solving them and I think you might of underestimated the task sadly id scrap what you've got. Its important to have a clear idea in your head of how your going to solve it before writing code which I know is easier said than done when your learning the language as well as the problem I don't know Python but I can tell by your code that you're a beginner and thats going to make life more difficult. Google "functions in python" you should understand them before attempting this as they will be very usefull.

The bruteforce approach is the most obvious but its going to perform badly.
1. Write a function that can determine if a Sudoku is correct. I'm sure you know the rules 2. Increment numbers of the cell's to generate all potential solutions whilst calling the function to check if one is valid.

I hear the go-to solution for Sudoku solving is a backtracking algorithm which would solve it much faster with far less steps than a dumb-bruteforce. I've never written one so can't advise you but its worth a google if you want to do it properly.
0
#3
I implemented my code into functions and simplified it. This is what I have got and I don't know where to go from here.

invalidGrid = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]] validGrid = [[1,4,3,2], [3,2,1,4], [2,3,4,1], [4,1,2,3]] def solveSudoku(sudokuGrid): if checkRows(sudouGrid) == True: print 'Rows are correct' if checkColums(sudokuGrid) == True: print 'Rows are correct' if checkSquares(sudokuGrid) == True: return True def checkRows (grid): for row in grid: if isValidRow(row) == False: return False return True def isValidRow(row): requiredNumbers = [1,2,3,4] for number in requiredNumbers: if number not in row: return False return True def checkColumns(grid): column = [] for index in range(4): newColumn = [] for row in grid: newColumn.append(row[index]) column.append(newColumn) return checkRows(column) def checkSquares (gridFourByFour): rowsOfSquares = makeSquares(gridFourByFour) for rowOfSquares in rowOfSquares: for square in rowOfSquares: if checkSquare(square) == False: return False return True print checkSudoku(validGrid)
0
X

new posts Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (554)
33.95%
Yes, I like the idea of applying to uni after I received my grades (PQA) (674)
41.3%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (330)
20.22%
I think there is a better option than the ones suggested (let us know in the thread!) (74)
4.53%