修复Python递归会导致错误的结果

2024-10-03 23:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我以为我的代码没有问题,但当我开始更改值时,我最终遇到了递归问题。我以为我修好了,但当我看结果时,他们都错了。当我保持递归时,结果是好的。你知道吗

我使用while循环来尝试解决递归问题,在这里,我不是递归地调用spread方法,而是将传递给它的值返回给propagate方法,如果它不传递值,则返回False。因此,只要方法继续返回值,它就应该使用上一次运行的结果重新运行spread方法。你知道吗

此代码在突破递归限制之前一直有效:

    def spread(self, position):
        for direction in self._directions:
            (x, y) = self.changePosition(position, direction)
            if self.canInfectMatrix[x][y] and not self.contactMatrix[x][y]:
                self.contactMatrix[x][y] = True
                self.spread([x,y])
#                 return [x,y]
#             return False

    def propagate(self):
        # initialize canInfectMatrix and contactMatrix
        self.contactMatrix = [[False for row in range(self.cardinalWidth)] for col in range(self.cardinalWidth)]
        self.canInfectMatrix = [[False for row in range(self.cardinalWidth)] for col in range(self.cardinalWidth)]
        for col in range(self.cardinalWidth):
            for row in range(self.cardinalWidth):
                self.canInfectMatrix[row][col] = self.getsInfected(self._matrix[col][row])
        # Spread infection.
        for x in range(self.cardinalWidth):
            for y in range(self.cardinalWidth):
                if self._matrix[x][y] == "infected":
                    self.spread([x,y])
#                     position = [x,y]
#                     while position:
#                         position = self.spread(position)

以下代码不起作用,但没有错误:

    def spread(self, position):
        for direction in self._directions:
            (x, y) = self.changePosition(position, direction)
            if self.canInfectMatrix[x][y] and not self.contactMatrix[x][y]:
                self.contactMatrix[x][y] = True
#                self.spread([x,y])
                 return [x,y]
             return False

    def propagate(self):
        # initialize canInfectMatrix and contactMatrix
        self.contactMatrix = [[False for row in range(self.cardinalWidth)] for col in range(self.cardinalWidth)]
        self.canInfectMatrix = [[False for row in range(self.cardinalWidth)] for col in range(self.cardinalWidth)]
        for col in range(self.cardinalWidth):
            for row in range(self.cardinalWidth):
                self.canInfectMatrix[row][col] = self.getsInfected(self._matrix[col][row])
        # Spread infection.
        for x in range(self.cardinalWidth):
            for y in range(self.cardinalWidth):
                if self._matrix[x][y] == "infected":
#                    self.spread([x,y])
                     position = [x,y]
                     while position:
                         position = self.spread(position)

注意每个方法底部注释的变化

据我所知,这两个函数应该完成相同的事情,但它们没有完成。在我得到递归限制错误之前,其中一个函数非常有效。另一个根本不起作用,但我没有递归错误。你知道吗

为什么它们会返回不同的值?你知道吗


Tags: 方法inselffalsefordefpositionrange
1条回答
网友
1楼 · 发布于 2024-10-03 23:28:01

在第二个版本中,您将在for循环中使用return语句。当然,这样的返回会中断for循环,而for循环永远不会恢复。你知道吗

相反,您希望调用spread()返回点列表,可能为空。然后在调用者中,将这些新答案附加到要处理的点的列表中。调用者将通过反复从列表中弹出一个项目,调用spread(),并将它得到的所有新点追加到列表中,然后重复直到列表为空。你知道吗

相关问题 更多 >