0
def return_solved_board(board):
    solution = board.copy()

Backtracking recursion loop starts

def solve(board): 
    for y in range(9):
        for x in range(9):
            if solution[y][x] == 0:
                for number in range(1,10):
                    if check_rules_at_point(x, y, number, solution):
                        solution[y][x] = number
                        solve(solution)
    #Where backtracking comes into play, if the solve method fails on the next cell than it resets current one.
                        solution[y][x] = 0
    #Breaks out of recursion to try a differant number in cell prior
                return
    #Stores a solved copy of the solution into global variable
    global returned_information
    returned_information = solution.copy()
    print(returned_information) #1st print statement

Checks if the board can be solved or not

if not check_for_valid_board(board):
    return "This board is not possible to solve."
else:
    solve(board)
    global returned_information
    print(returned_information) #2nd print statement
    return returned_information

print(return_solved_board(valid_sudoku_board1))

I am trying to write a program to solve sudoku puzzles. The puzzle is stored in a list of lists. When I run this program the first print statement returns the correct solved list of lists. However, the second return statement returns the original unsolved puzzle. Both of their id's are the same however I can't figure out why returned_information in the second print statement changes back as returned_information is only called once.

Andy K
  • 4,944
  • 10
  • 53
  • 82
atl15
  • 3
  • 1
  • Welcome to SO! More code seems necessary even though I guess you have an answer. What is `board` and what is `solution`? `board` is never used in `solve` and `solution` is never initialized or declared. Thanks for clarifying – ggorlen May 20 '20 at 17:58
  • I'm pretty new to programming and I believe some of these may be bad practices. Board is an argument with a 2D array (9x9). I have since realized that the need solution was redundant after you pointed it out as I had misunderstood how variable assignment and copy method worked. Thanks for the feedback! – atl15 May 21 '20 at 18:28

1 Answers1

1

You're probably bumping into dicts'/lists' copy() being a shallow copy; use copy.deepcopy(...) instead to do a deep copy.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    [deepcopy has awful performance](https://stackoverflow.com/questions/3043369/deepcopy-and-python-tips-to-avoid-using-it). Something like `[x[:] for x in board]` should be ~20 times faster although I have no idea what sort of structure it is (I assume 2d list). See also [1](https://stackoverflow.com/questions/24756712/deepcopy-is-extremely-slow), [2](https://stackoverflow.com/questions/35008473/avoid-deepcopy-due-to-performance) – ggorlen May 20 '20 at 17:59
  • Good point. Deepcopy is the generic tool; use a more specific tool if you can, and need to performance-wise. – AKX May 20 '20 at 18:30