The Student Room Group

2 by 2 sudoku algorithm (python)

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 == (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)
Sorry you've not had any responses about this. :frown: Are you sure you've posted in the right place? :smile: Here's a link to our subject forum which should help get you more responses if you post there. :redface:


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

Spoiler

Original post by 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.To be honest, you're probably going to be better off starting again than trying to sort out what you've done.

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)






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!





Notes: for a robust solver, it's generally a bad idea to introduce random numbers - it's better to iterate over the possibilities.
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 TSR Jessica
Sorry you've not had any responses about this. :frown: Are you sure you've posted in the right place?FWIW, I would have thought this belongs in F119: https://www.thestudentroom.co.uk/forumdisplay.php?f=119
(edited 7 years ago)

Quick Reply

Latest