# 2 by 2 sudoku algorithm (python)

Watch
Announcements

Page 1 of 1

Go to first unread

Skip to page:

hello,

I am writing an algorithm that solves a 2 by 2 Sudoku puzzle. I haven't fully competed it yet and my code is very messy and complicated. I wondered if anyone can help sort my code out. You help will be much appreciated.

thanks!

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 [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)

I am writing an algorithm that solves a 2 by 2 Sudoku puzzle. I haven't fully competed it yet and my code is very messy and complicated. I wondered if anyone can help sort my code out. You help will be much appreciated.

thanks!

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 [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)

0

reply

Report

#2

Sorry you've not had any responses about this. Are you sure you've posted in the right place? Here's a link to our subject forum which should help get you more responses if you post there.

Just quoting in Danny Dorito so she can move the thread if needed

Just quoting in Danny Dorito so she can move the thread if needed

Spoiler:

Show

(Original post by

x

**Danny Dorito**)x

0

reply

Report

#3

(Original post by

hello,

I am writing an algorithm that solves a 2 by 2 Sudoku puzzle. I haven't fully competed it yet and my code is very messy and complicated. I wondered if anyone can help sort my code out. You help will be much appreciated.

**Spazum**)hello,

I am writing an algorithm that solves a 2 by 2 Sudoku puzzle. I haven't fully competed it yet and my code is very messy and complicated. I wondered if anyone can help sort my code out. You help will be much appreciated.

Some suggestions:

You would avoid a lot of the repetition in your code if you simply had

**one**4 x 4 grid for the entire puzzle.

The simplest possible implementation (which should still be perfectly adequate for a 4x4 puzzle - I've written a full 9x9 solver and it wasn't really much more complicated than this) would then be something like:

create a function isBoardValid that tests if your board is valid. This is probably the hardest thing to do, but you just need to check every row and column of your 4x4 puzzle for repeated pieces, as well as testing the 4 2x2 subgrids.

Then your main "solver" looks like (in psuedocode)

Code:

solveBoard: set finished = false loop over all the squares in the grid. if (squareIsEmpty) for digit = 1 to 4 set square to digit if (isBoardValid) solveBoard # note: recursive function call! set square to empty set finished = false if finished print board # there were no empty squares, so you're done!

When you make a "trial move", you need to make sure you "unmake" it after you've analysed it.

You really want to be using recursion for a problem like this - it may seem more complicated at first, but it reduces the amount of work/bookkeeping you need to do in the long run.

(Original post by

Sorry you've not had any responses about this. Are you sure you've posted in the right place?

**TSR Jessica**)Sorry you've not had any responses about this. Are you sure you've posted in the right place?

0

reply

X

Page 1 of 1

Go to first unread

Skip to page:

### Quick Reply

Back

to top

to top