0

I know that return statement will make the execution jump out of the function. So I have a concern with the following program which solves Suduko puzzle recursively:

class Puzzle:
    def __init__(self):
        self.loop = 0
        self.answer = 0
        self.done = False

    def same_row(self,i,j):
        return i/9 == j/9

    def same_col(self,i,j):
        return (i-j)%9 == 0

    def same_block(self,i,j):
        return (i/27 == j/27 and i%9/3 == j%9/3)

    def print_format(self,a):
        for row in range(9):
            for col in range(9):
                print '%s ' % a[9*row + col],
            print ''

    def solve(self,a):
        self.loop += 1
        if self.answer == 1 and self.loop>100000 or self.answer == 2:
            self.done = True

        i = a.find('0')
        if i == -1:
            self.answer += 1
            print 'after %d loops worked out solution %d:' % (self.loop, self.answer)
            self.print_format(a)
            return

        excluded_num = set()

        for j in range(81):
            if self.same_row(i,j) or self.same_col(i,j) or self.same_block(i,j):
                excluded_num.add(a[j])

        for m in '123456789':
            if m not in excluded_num:
                if self.done:
                    return
                print 'xxx loop %d' % self.loop
                self.solve(a[:i]+m+a[i+1:])

if __name__ == '__main__':
    puz = Puzzle()
    sudoku="060593000901000500030400090108020004400309001200010609080006020004000807000000000"
    puz.solve(sudoku)

This is an program to solve a sudoku puzzle, I check the output I find a question I cannot understand why, the output is something like:

output screenshot output screenshot Could anyone tell me why the 'xxx loop 125' and 'xxx loop 175' are getting printed? As there is return statement in line 32, why the execution still went down and gets them printed? Many thanks

1
  • getting more confused when putting that statement:xxx loop 174 after 175 loops worked out solution 2: 7 6 2 5 9 3 1 4 8 9 4 1 2 7 8 5 3 6 8 3 5 4 6 1 7 9 2 1 9 8 6 2 5 3 7 4 4 7 6 3 8 9 2 5 1 2 5 3 7 1 4 6 8 9 5 8 7 1 4 6 9 2 3 3 1 4 9 5 2 8 6 7 6 2 9 8 3 7 4 1 5 xxx loop 175 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 inside loop 176 Commented Jan 14, 2014 at 16:15

2 Answers 2

2
for m in '123456789':
    if m not in excluded_num:
        if self.done:
            return
        print 'xxx loop %d' % self.loop
        self.solve(a[:i]+m+a[i+1:])

This loop will iterate 9 times (m==1, m==2, m==3 ... m==9). Even if it completes the solve with m==3, it still goes through 6 more iterations where it might run that print statement.

As an aside, this may work better if you define the Puzzle to be each sudoku problem. Initialize it with your sudoku string, and use that as self.unsolved=pattern. It makes a bit more sense in my head that way, than simple as a machine that solves puzzles. Right now it's less a class Puzzle and more a class PuzzleSolvingMachine

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

3 Comments

Give me some time -- I'm running through the logic of your program. I'm kinda rusty with self-recursing functions.
I don't have the same output as you -- are you sure this is the same code? When I copy/paste this, it does not correctly solve the sudoku. This is the output I get
Ah ha! I figured out the error. I was running it in Python3 so 25/10 == 2.5 instead of as in Python2 where 25/10 == 2. I switched to floor division and it's working fine now. Let me keep running through it......
1

Because done is not set. Not all your calls to solve have returned. Some are sitting at the recursive solve step. You print out your after... then return, to the previous solve call. Then they continue on your merry way.

Comments

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.