如何停止两个递归堆栈同时中断?

时间:2019-04-14 16:21:04

标签: python python-3.x recursion

我写了一个实现递归的python程序。问题是,通过调试器跟踪堆栈后,倒数第二个和最后一个堆栈立即退出,这违反了我的逻辑。

我尝试了事件记录和跟踪代码。我还研究了其他一些问题和答案,但是没有人能够回答这种奇怪和独特的情况。

def abc(self, node_position, path=[]):
        print(
            "Node position called is {} and path given is: {}".format(
                node_position, path
            )
        )
        connected_nodes = self.graph[node_position]
        for connected_node in connected_nodes:
            if connected_node == self.final_destination:
                print(
                    "connected_node is the final destination, {}".format(
                        self.final_destination
                    )
                )
                path.append(connected_node)
                return (True, path)
            if self.has_connected_nodes(connected_node):
                path.append(connected_node)
                check, path = self.abc(connected_node, path)
                if not check:
                    path[:-1]
                    return (False, path)
                print("Final path formed after an iteration is {}".format(path))
                self.paths.append(path)
        print("The current node position is {}".format(node_position))

预期结果是node_position=1堆栈返回到node_position=0堆栈。但是,node_position=1堆栈直接返回到基本函数调用。完整的代码是:

class Solution(object):

    def has_connected_nodes(self, connected_node):
        if len(self.graph[connected_node]) > 0:
            return True
        return False

    def abc(self, node_position, path=[]):
        print(
            "Node position called is {} and path given is: {}".format(
                node_position, path
            )
        )
        connected_nodes = self.graph[node_position]
        for connected_node in connected_nodes:
            if connected_node == self.final_destination:
                print(
                    "connected_node is the final destination, {}".format(
                        self.final_destination
                    )
                )
                path.append(connected_node)
                return (True, path)
            if self.has_connected_nodes(connected_node):
                path.append(connected_node)
                check, path = self.abc(connected_node, path)
                if not check:
                    path[:-1]
                    return (False, path)
                print("Final path formed after an iteration is {}".format(path))
                self.paths.append(path)
        print("The current node position is {}".format(node_position))

    def allPathsSourceTarget(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: List[List[int]]
        """
        self.graph = graph
        self.final_destination = len(graph) - 1
        self.paths = []
        self.abc(0, [])
        self.final_paths = []
        for path in self.paths:
            path = [0] + path
            self.final_paths.append(path)
        return self.final_paths


solution = Solution()
solution.allPathsSourceTarget([[1, 2, 3], [2], [3], []])

0 个答案:

没有答案