Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting multiple return statements into different functions

I'm implementing a backtracking solution and have multiple return statements. I don't see a way to split this into multiple functions, so there is only one return statement per function. The code is..

  def solve_grid(self, grid, row=0, col=0):


    row, col = self.find_next(grid, row, col)
    if row == -1:
        return True
    for num in range(1,10):
        if self.isValid(grid, row, col, num):
            grid[row][col] = num
            if self.solve_grid(grid, row, col):
                return True
            grid[row][col] = 0
    return False

I;ve tried splitting it up as follows

def check(self, grid, row, col):
    boolean = None
    row, col = self.find_next(grid, row, col)
    if row == -1:
        boolean = True
    return boolean

def solve_grid(self, grid, row=0, col=0):

    boolean = None
    if not self.check(grid, row, col):
        for num in range(1,10):
            if self.isValid(grid, row, col, num):
                grid[row][col] = num
                if self.solve_grid(grid, row, col):
                    boolean = True
                else: 
                    boolean = False
            grid[row][col] = 0
    return boolean

This results in a maximum recursion depth. I'm a bit lost a to how to go about this, I've never really had to try to split multiple return statements before. Any pointers or tips would be helpful.


1 Answers

If all you want to do is remove the multiple returns, this will do it

def solve_grid(self, grid, row=0, col=0):
    row, col = self.find_next(grid, row, col)
    if row == -1:
        result = True
    else:
        result = False
        for num in range(1,10):
            if self.isValid(grid, row, col, num):
                grid[row][col] = num

                if self.solve_grid(grid, row, col):
                    result=True
                    break

                grid[row][col] = 0

    return result

You could also convert the for loop into a while to remove the break

def solve_grid(self, grid, row=0, col=0):
    row, col = self.find_next(grid, row, col)
    if row == -1:
        result = True
    else:
        result = False
        num = 0
        while num < 9 and not result:
            num += 1
            if self.isValid(grid, row, col, num):
                grid[row][col] = num

                if self.solve_grid(grid, row, col):
                    result=True
                else:
                    grid[row][col] = 0

    return result

But I personally find your original form to be more readable.

One final simplification gets rid of a level of indentation by initializing result with the check of row

def solve_grid(self, grid, row=0, col=0):
    row, col = self.find_next(grid, row, col)
    result = (row == -1)
    num = 0
    while num < 9 and not result:
        num += 1
        if self.isValid(grid, row, col, num):
            grid[row][col] = num

            if self.solve_grid(grid, row, col):
                result=True
            else:
                grid[row][col] = 0


    return result

And now, I think it's fairly clean

like image 90
kdopen Avatar answered Jul 30 '26 09:07

kdopen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!