The Student Room Group

2 by 2 sudoku algorithm (python)

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 == (0):
== random.randint (1,4) # change 0 with a random (1,4) number
while == :
== random.randint (1,4)

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

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

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

for i in range (1,4):
while [0] or [1] in (gridOne) == [0] or [1] in (gridTwo): # if top row in grid 1 and 2 has a repeated number
[1] in (gridOne) == random.randint (1,4) # change the duplicated number
[0] 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
[1] in (gridOne) == random.randint (1,4)
[0] in (gridTwo) == random.randint (1,4)

while [2] or [3] in (gridOne) == [2] or [3] in (gridTwo): # if bottom row in grid 1 and 2 has a repeated number
[3] in (gridOne) == random.randint (1,4) # change the duplicated number
[2] in (gridTwo) == random.randint (1,4)
[3] 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
[3] in (gridOne) == random.randint (1,4)
[2] in (gridTwo) == random.randint (1,4)
[3] in (gridTwo) == random.randint (1,4)

while [0] or [1] in (gridThree) == [0] or [1] in (gridFour): # if top row in grid 3 and 4 has a repeated number
[0] 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
[0] in (gridThree) == random.randint (1,4)

while [2] or [3] in (gridThree) == [2] or [3] in (gridFour): # if bottom row in grid 3 and 4 has a repeated number
[2] in (gridThree) == random.randint (1,4) # change the duplicated number
[2] in (gridFour) == random.randint (1,4)
[3] 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
[2] in (gridThree) == random.randint (1,4)
[2] in (gridFour) == random.randint (1,4)
[3] in (gridFour) == random.randint (1,4)


while [0] or [2] in (gridOne) == [0] or [2] in (gridThree): # if first column has a repeated number
[0] in (gridThree) == random.randint (1,4) # change the duplicated number
[2] 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
[0] in (gridThree) == random.randint (1,4)
[2] in (gridThree) == random.randint (1,4)

while [1] or [3] in (gridOne) == [1] or [3] in (gridThree): # if second column has a repeated number
[1] in (gridOne) == random.randint (1,4) # change the duplicated number
[3] 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
[1] in (gridOne) == random.randint (1,4)
[3] in (gridOne) == random.randint (1,4)

while [0] or [2] in (gridTwo) == [0] or [2] in (gridFour): # if third column has a repeated number
[0] in (gridtwo) == random.randint (1,4) # change the duplicated number
[2] in (gridTwo) == random.randint (1,4)
[2] 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
[0] in (gridtwo) == random.randint (1,4)
[2] in (gridTwo) == random.randint (1,4)
[2] in (gridFour) == random.randint (1,4)

while [1] or [3] in (gridTwo) == [1] or [3] in (gridFour): # if fourth column has a repeated number
[3] in (gridTwo) = random.randint (1,4) # change the duplicated number
[3] 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
[3] in (gridTwo) = random.randint (1,4)
[3] 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!
Reply 1
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 :smile: 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 :smile:

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 :smile:
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.
(edited 7 years ago)
Reply 2
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)

Quick Reply

Latest

Trending

Trending