Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将多个返回语句拆分为不同的函数_Python_Recursion - Fatal编程技术网

Python 将多个返回语句拆分为不同的函数

Python 将多个返回语句拆分为不同的函数,python,recursion,Python,Recursion,我正在实现一个回溯解决方案,并且有多个返回语句。我看不到将其拆分为多个函数的方法,因此每个函数只有一个return语句。代码是 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

我正在实现一个回溯解决方案,并且有多个返回语句。我看不到将其拆分为多个函数的方法,因此每个函数只有一个return语句。代码是

  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
一,;我试着把它分成以下几部分

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

这将导致最大递归深度。我有点不知道该怎么做,我以前从未真正尝试过拆分多个返回语句。任何指针或提示都会很有帮助。

如果您只想删除多个返回,这就可以了

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
您还可以将
for
循环转换为
while
以删除
中断

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

现在,我认为它相当干净

我不确定在一个函数中有多个return语句在一般情况下是件坏事。您的原始代码中是否也出现堆栈溢出错误?您正在达到最大递归深度,因为每次
isValid()
调用成功时,您都会再次调用
solve\u grid()
。如果
isValid()
在合理深度失败,这将作为基本情况。我不确定这本质上是错误的。我不仅仅是深度错误,是的,我意识到这是在检查数独网格,并试图通过回溯原始作品来解决它,但当试图拆分返回语句时,这种情况就会发生。那么我该如何重新安排呢?多重返回语句我们正在阻止这种情况并处理这些案例。至于为了可读性而试图将其拆分的多个返回语句,我从来都不喜欢多个返回。我能看到的唯一区别是,
grid[row][col]=0
的缩进(以及语义)不同。这是故意的吗?这是一个布尔值为
None
False
True
的问题吗?
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