Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
building a sudoku solver
#8
The loop in place_random() ends when it finds a number that can be placed in the cell, but what happens if there are no valid numbers?
1
2
3
4
5
6
def place_random(x,y):
    n = random.sample(range(1,10),9)
    i = 0
    while not possible(x,y,n[i]):  # <- What is the value for i if there are no possible solutions for this cell?
        i += 1
    grid[x][y] = n[i]
This is ugly code anyway. Why are you using while?
1
2
3
4
5
6
def place_random(x, y):
    for n in random.sample(range(1, 10), 9)
        if possible(x, y, n):
            grid[x][y] = n
            return True
    return False
Returns True if the cell is filled, else False.

Not that it matters much since you cannot generate sudoku puzzles using your approach. Most of the puzzles your generator makes will end up being invalid. You will need to make a backtracking algorithm similar to your solver that backs up when it encounters an invalid puzzle and tries again.
Reply


Messages In This Thread
building a sudoku solver - by usercat123 - Sep-27-2021, 03:07 PM
RE: building a sudoku solver - by deanhystad - Sep-27-2021, 03:32 PM
RE: building a sudoku solver - by usercat123 - Sep-27-2021, 03:38 PM
RE: building a sudoku solver - by deanhystad - Sep-27-2021, 03:51 PM
RE: building a sudoku solver - by usercat123 - Sep-28-2021, 01:45 PM
RE: building a sudoku solver - by deanhystad - Sep-28-2021, 03:25 PM
RE: building a sudoku solver - by usercat123 - Oct-01-2021, 03:32 PM
RE: building a sudoku solver - by deanhystad - Oct-01-2021, 08:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 3,966 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Sudoku Solver, please help to solve a problem. AdithyaR 5 3,218 Oct-28-2021, 03:15 PM
Last Post: deanhystad
  unable to use result of solver in another function ross1993hall 0 1,813 Aug-10-2020, 10:29 AM
Last Post: ross1993hall
  Help with sudoku Mondata 4 3,117 Apr-13-2020, 12:35 AM
Last Post: deanhystad
  Trouble with Sudoku Solver Techmokid 2 2,722 Apr-08-2020, 07:55 AM
Last Post: Techmokid
  editing lists / sudoku solver monagro 5 5,266 May-29-2018, 02:16 PM
Last Post: monagro
  Word Search Solver PythonLamer 4 6,263 Oct-12-2017, 06:13 PM
Last Post: nilamo
  Need help designing a multi-threaded solver 4Dummies 8 7,462 Jun-18-2017, 08:39 PM
Last Post: 4Dummies

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020