Created
April 4, 2018 21:32
-
-
Save tugloo1/a299784aba184f7d98ce94e3c15bc3ca to your computer and use it in GitHub Desktop.
Revisions
-
tugloo1 created this gist
Apr 4, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ def get_nodes_parent(tree, node_to_evaluate): for i, row in enumerate(tree): if row[node_to_evaluate] == 1: return i else: return None def evaluate_nodes_path(tree, node_to_evaluate, root): paths = [] current_node = node_to_evaluate while True: parent = get_nodes_parent(tree, current_node) if parent == root: paths.append(parent) return paths elif parent is None: return paths else: paths.append(parent) current_node = parent def question4(tree, root, node1, node2): node1_path = evaluate_nodes_path(tree, node1, root) node2_path = evaluate_nodes_path(tree, node2, root) if node1_path.__len__() < node2_path.__len__(): for node in node1_path: if node in node2_path: return node else: for node in node2_path: if node in node1_path: return node a = question4([[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0]], 3, 1, 4) print(a)