Skip to content

Instantly share code, notes, and snippets.

@tugloo1
Created April 4, 2018 21:32
Show Gist options
  • Save tugloo1/a299784aba184f7d98ce94e3c15bc3ca to your computer and use it in GitHub Desktop.
Save tugloo1/a299784aba184f7d98ce94e3c15bc3ca to your computer and use it in GitHub Desktop.

Revisions

  1. tugloo1 created this gist Apr 4, 2018.
    48 changes: 48 additions & 0 deletions whatevs.py
    Original 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)