Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:51:35+00:00 2026-06-07T13:51:35+00:00

My sudoku solver does exactly what it’s supposed to do – except returning the

  • 0

My sudoku solver does exactly what it’s supposed to do – except returning the correct thing. It prints what it should right before the return (a correctly solved grid), but then it seems to keep running for a bit and returns None. I can’t figure out what’s going on.

Grid is a list of lists. Assume that check_sudoku returns True if the grid is valid (solved or not), and False otherwise.

def solve_sudoku(grid, row=0, col=0):
    "Searches for valid numbers to put in blank cells until puzzle is solved."
    # Sanity check
    if not check_sudoku(grid): 
        return None
    # Sudoku is solved
    if row > 8: 
        return grid
    # not a blank, try next cell
    elif grid[row][col] != 0:
        next_cell(grid, row, col)
    else:
        # try every number from 1-9 for current cell until one is valid
        for n in range(1, 10):
            grid[row][col] = n
            if check_sudoku(grid):
                next_cell(grid, row, col)
        else:
            # Sudoku is unsolvable at this point, clear cell and backtrack
            grid[row][col] = 0
            return

def next_cell(grid, row, col):
    "Increments column if column is < 8 otherwise increments row"
    return solve_sudoku(grid, row, col+1) if col < 8 else solve_sudoku(grid, row+1, 0)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:51:38+00:00Added an answer on June 7, 2026 at 1:51 pm

    It seems to me like this will never actually return something useful. The first time we enter your solve_sudoku, you check to see if the grid is solved, and if so you return it. After that, you begin a bunch of recursion which ultimately will end up coming back out towards the end of the function and returning None. No matter what, you are returning None all the time. The only thing that you end up with is a modified grid argument that you passed in to start.

    def solve_sudoku(grid, row=0, col=0):
        # possible return at the first entry point
        if not check_sudoku(grid): 
            return None
    
        # only time you would ever NOT get None
        if row > 8: 
            return grid
    
        ...recurse...
    
        # come back out when the stack unwinds,
        # and there is no return value other than None
    

    What I speculate is happening, is that you are printing the values along the way, and you do properly see a solved grid at the moment it happens, but your function isn’t set up to properly completely break out when that is done. It continues to loop around until it exhausts some range and you see a bunch of extra work.

    The important thing to do is to check the return value of the recursive call. You would either return None if its not solved, or grid if it is. As it stands, you never care about the result of your recursions in the calling scope.

    Because I don’t have all the details about your specific code, here is a simple equivalent:

    def solver(aList):
        if aList[0] == 10:
            print "SOLVED!"
            return None
    
        print "NOT SOLVED..."
        return next(aList)
    
    def next(aList):
        # do some stuff
        # check some stuff
        aList[0] += 1
        return solver(aList)
    
    
    if __name__ == "__main__":
        data = [0]
        solver(data)
        print data
    

    Notice that the indirectly recursive call to checker() -> solver() has its value returned all the way up the chain. In this case we are saying None means solved, and otherwise it should keep recursing. You know that at some point deep in your recursion stack, the solution will be solved. You will need to communicate that back up to the top right away.

    The communication looks like this:

    aList == [0]
    solver(aList)
      next(...)
        next(...)
          next(...)
            next(...) 
              #SOLVED
            <- next(...)
          <- next(...)
        <- next(...)
      <- next(...)
    <-solver(aList)
    aList == [10]
    

    And here is what your version might look like if applied to my simple example:

    aList == [0]
    solver(aList)
      next(...)
        next(...)
          # SOLVED
          next(...)
            next(...) 
              ...
            <- next(...)
          <- next(...)
        <- next(...)
      <- next(...)
    <-solver(aList)
    aList == [10]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a Sudoku solver using human methods. I have a list of strategies
Four years ago I wrote a Sudoku puzzle solver, and now I'm trying to
I programmed a sudoku solver in Java for a homework, and I am currently
i'm trying to write a Sudoku solver which will return only the first possible
I'm working on a Sudoku solver at school and we're having a little performance
I am a building a console Sudoku Solver where the main objective is raw
I've recently completed an algorithmic sudoku solver in C, one in which three different
I'm doing a Sudoku solver and for that I want my JTextFields to only
I have written a sudoku solver in Haskell. It goes through a list and
I was thinking of making a Sudoku solver, I have 2 questions: 1) What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.