Spazum
Badges: 6
Rep:
?
#1
Report Thread starter 3 years ago
#1
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)
0
reply
TSR Jessica
Badges: 19
Rep:
?
#2
Report 3 years ago
#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 :wizard:
Spoiler:
Show
(Original post by Danny Dorito)
x
0
reply
DFranklin
Badges: 18
Rep:
?
#3
Report 3 years ago
#3
(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)

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!
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. 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
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

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

Personalise

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

No, I think predicted grades should still be used to make offers (641)
33.54%
Yes, I like the idea of applying to uni after I received my grades (PQA) (806)
42.18%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (378)
19.78%
I think there is a better option than the ones suggested (let us know in the thread!) (86)
4.5%

Watched Threads

View All
Latest
My Feed

See more of what you like on
The Student Room

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

Personalise