Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:36:29+00:00 2026-06-01T09:36:29+00:00

This function is driving me insane! def CCAD1 (tree) leaves = [] for otu

  • 0

This function is driving me insane!

def CCAD1 (tree)
    leaves = []
    for otu in tree:
        if tree[otu][2]== None and tree[otu][1]== None:
                leaves += [otu]
    ccad = {}
    for leaf in leaves:
            otuX = leaf
            otu1 = leaf
            for leaf2 in leaves:
                otuY = leaf2
                otu2 = leaf2
                while tree[otu1][0] is not None and tree[otu2][0] is not None and tree[otu1][0][0] != tree[otu2][0][0]:
                        otu1,otu2,tree = tree[otu1][0][0],tree[otu2][0][0],tree
                if tree[otu1][0] is not None:
                    ccad[otuX] = {otuY:tree[otu1][0]}
    return ccad

This is the input to the function

{'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}

The output should be in the structure of something like {"A":{"B":("AB",4)}}, in the code above this is the dictionary ‘CCAD’. I’ve literally been trying all night to do it but it’s not working and I have no idea why.

Basically what I’m trying to do is to build a function that will output a dictionary of dictionaries where for each distinct pair of elements in the list leaves it will calculate an ancestor (I got some great help calculating that previously here) along with the distance, which in that link there would be a matter of keeping a running total each time the function iterates.

It’s outputting a dictionary of dictionaries like I need, but it’s not doing it for every pair, only certain ones. The ‘tree’ data structure is in that link too if you need to see it.

Any help is appreciated, I’m getting pretty desperate at this stage :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:36:31+00:00Added an answer on June 1, 2026 at 9:36 am

    Okay, I think you want to calculate the distances between every leaf node. So I’m having a go at solving your problem, not answering your question.

    Your commonAncestor algorithm is flawed as it assumes that the leaf nodes are all at the same depth. They are not.

    The first solution that springs to mind is to determine all of the leaf nodes and calculate the path to the root node for each of them. Determine the closest common ancestor by comparing both paths in reverse.

    The output from this is a dictionary of pairs of nodes and the number of hops between them.

    from itertools import combinations
    
    data = {'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}
    
    def get_path(tree,leaf):
      path = []
      location = leaf
      while True:
        path.append(location)
        parent = tree.get(location)[0]
        if parent:
          location = parent[0]
        else:
          break
      return path
    
    def get_leaves(tree):
      return [ x for (x,y) in tree.items() if y[1] is None and y[2] is None ]
    
    def leafDistances(tree):
      paths = {}
      leaves = get_leaves(tree)
      for leaf in leaves:
        paths[leaf] = get_path(tree,leaf)
      results = {}
      for l1,l2 in combinations(leaves,2):
        commonAncestor = [ x for (x,y) in zip(paths[l1][::-1],paths[l2][::-1]) if x == y ][-1]
        distance = paths[l1].index(commonAncestor) + paths[l2].index(commonAncestor)
        results[(l1,l2)] = distance
        print "%s <-> %s Ancestor == %s, distance == %s\nPath of %s == %s\nPath of %s == %s" % (l1,l2,commonAncestor,distance,l1,paths[l1],l2,paths[l2])
      return results
    
    leafDistances(data)
    

    This prints out for clarity:

    A <-> C Ancestor == ADBFGC, distance == 4
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    A <-> B Ancestor == ADBFG, distance == 5
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    A <-> E Ancestor == ADBFGCE, distance == 5
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of E == ['E', 'ADBFGCE']
    A <-> D Ancestor == AD, distance == 2
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    A <-> G Ancestor == ADBFG, distance == 4
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    A <-> F Ancestor == ADBFG, distance == 5
    Path of A == ['A', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    C <-> B Ancestor == ADBFGC, distance == 5
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    C <-> E Ancestor == ADBFGCE, distance == 3
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    Path of E == ['E', 'ADBFGCE']
    C <-> D Ancestor == ADBFGC, distance == 4
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    C <-> G Ancestor == ADBFGC, distance == 4
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    C <-> F Ancestor == ADBFGC, distance == 5
    Path of C == ['C', 'ADBFGC', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    B <-> E Ancestor == ADBFGCE, distance == 6
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of E == ['E', 'ADBFGCE']
    B <-> D Ancestor == ADBFG, distance == 5
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    B <-> G Ancestor == BFG, distance == 3
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    B <-> F Ancestor == BF, distance == 2
    Path of B == ['B', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    E <-> D Ancestor == ADBFGCE, distance == 5
    Path of E == ['E', 'ADBFGCE']
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    E <-> G Ancestor == ADBFGCE, distance == 5
    Path of E == ['E', 'ADBFGCE']
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    E <-> F Ancestor == ADBFGCE, distance == 6
    Path of E == ['E', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    D <-> G Ancestor == ADBFG, distance == 4
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    D <-> F Ancestor == ADBFG, distance == 5
    Path of D == ['D', 'AD', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    G <-> F Ancestor == BFG, distance == 3
    Path of G == ['G', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    Path of F == ['F', 'BF', 'BFG', 'ADBFG', 'ADBFGC', 'ADBFGCE']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me insane, this is HTML code. <html> <head> <script> function init()
This is driving me insane. I have a JS file that is: Ext.onReady(function(){ Ext.QuickTips.init();
This is driving me mad. I'm passing two variables into a function that posts
I have a javascript function that has been driving me nuts. This is the
This has been driving me insane all day and I think I have the
This is driving me nuts, I have a login function that checks to make
This is driving me insane now. I have a loop that creates buttons. Each
This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are:
This function is written in ActionScirpt. What kind of decryption this is? Is there
This function of mine keeps on failing an autograder, I am trying to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.