0
def solver():
    empty_cell = empty_square()
    counter = 0

    if not empty_cell:
        return True

    i,j = empty_cell[0],empty_cell[1]
    for num in range(1,10):
        counter += 1
        if constraint_check(num,i,j):
            sudoku[i][j] = num

            if solver():
                return True

            else:
                sudoku[i][j] = 0

    return False 

Given the code above, how would I implement a counter to count how many iterations the recursive part of the function makes? As can be seen in the code I have attempted something above but I was not able to retrieve this variable to print it out and record the number.

5
  • 1
    Have you searched for the problem? What did you find? Commented Jul 4, 2021 at 17:03
  • @CassandraTrotter if you've found a solution, why not share it as a self answer so that other people with the same problem can learn from your experience? The point of the site is to curate a repository of knowledge, not purge questions as soon as you get a solution. Commented Jul 5, 2021 at 1:15
  • I did not find a solution @ggorlen hence I deleted the contract. The answer provided is severely inadequete. Thus no good answers should enable me to delete the question Commented Jul 6, 2021 at 20:14
  • Hmm -- I might not have been clear. By asking a question, you create a community-owned resource for future visitors with the same problem. Not getting an answer you're satisfied with isn't grounds for deleting a question (unless no answers have upvotes, which is not applicable here). 3 community members felt the answer helped them or was an adequate solution, so the thread remains to help future users. See this meta thread header "When can't I delete my own post?" Commented Jul 6, 2021 at 20:28
  • BTW, Counting recursive calls of a function seems to be the canonical thread for this question. The top answer suggests initializing solver.counter = 0 outside of the function and changing your calls to solver.counter += 1. After all the calls resolve, solver.counter will hold your result in the enclosing scope. Commented Jul 6, 2021 at 21:41

1 Answer 1

3

set counter as function parameter -> solver(counter=1) then when calling it again within your function add +1 -> solver(counter+1)

Sign up to request clarification or add additional context in comments.

1 Comment

This answer doesn't work. I have found a seperate solution. Please delete this answer so I can delete the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.