Skip to content

Instantly share code, notes, and snippets.

@tugloo1
Created April 4, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugloo1/a299784aba184f7d98ce94e3c15bc3ca to your computer and use it in GitHub Desktop.
Save tugloo1/a299784aba184f7d98ce94e3c15bc3ca to your computer and use it in GitHub Desktop.
whatevssss
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment