Better ideas or ways to approach?

In summary, the conversation discusses a python code for a function that returns a subtree given a tree and a list of indices. The code uses a recursive approach and there may be alternative ways to write the code.
  • #1
ChrisVer
Gold Member
3,378
465
I was trying to finish the assignments of this link
https://ocw.mit.edu/courses/electri...ce-fall-2010/assignments/MIT6_034F10_lab0.pdf

unfortunately they are not numbered, but I dealt with the Tree reference one. In particular, given a tree TREE and a list of indices, it will return the appropriate subtree. My python code looks like this:
Python:
def depth(x):
    if not isinstance(x,(list,tuple)): return 0
    maxdep=0
    for element in x:
        maxdep = max( maxdep , depth(element) )
    return maxdep+1

def tree_ref( tree, index):
    #Error if you ask for a deeper subtree than available
    if len(index)>depth(tree):
        return "ERROR \t tree_ref(): Your tree is smaller than what you asked"
    #main return
    if len(index)==1: return tree[index[0]]
    for i in range(len(index)):
        return tree_ref(tree[index[i]], index[i+1:])

TREE=(((1, 2), 3), (4, (5, 6)), 7, (8, 9, 10))
print  tree_ref(TREE, (1,1,1))
As far as I've checked this code seems to work...Are there more clear or even optimal ways to do the job? thanks.
 
Technology news on Phys.org
  • #2
ChrisVer said:
Python:
    for i in range(len(index)):
        return tree_ref(tree[index[i]], index[i+1:])
This structure seems a little odd - I think it's equivalent to
Python:
return tree_ref(tree[index[0]],index[1:])
Also, you've used a recursive approach. I'd probably use a loop-based approach, since I expect it would handle larger trees without falling over.
 

FAQ: Better ideas or ways to approach?

1. What is the scientific method and how can it help generate better ideas or approaches?

The scientific method is a systematic approach to problem-solving that involves making observations, formulating a hypothesis, conducting experiments, and analyzing data. By following this method, scientists can generate better ideas or approaches as it allows for a structured and evidence-based approach to understanding and solving problems.

2. How can collaboration with other scientists lead to better ideas or approaches?

Collaboration with other scientists can lead to better ideas or approaches as it allows for the exchange of knowledge, perspectives, and skills. By working together, scientists can build on each other's ideas and push the boundaries of scientific understanding.

3. How can incorporating diverse perspectives and backgrounds lead to better ideas or approaches?

Incorporating diverse perspectives and backgrounds can lead to better ideas or approaches as it brings in a variety of viewpoints and experiences. This can lead to more creative and innovative solutions to problems as different individuals may approach a problem in unique ways.

4. What role does critical thinking play in generating better ideas or approaches?

Critical thinking is essential in generating better ideas or approaches as it involves questioning assumptions, evaluating evidence, and considering alternative perspectives. By critically analyzing a problem, scientists can develop more comprehensive and effective solutions.

5. How can continuously learning and staying updated with new research lead to better ideas or approaches?

Continuously learning and staying updated with new research can lead to better ideas or approaches as it allows scientists to build on previous knowledge and incorporate new findings into their work. This can lead to more informed and advanced ideas and approaches.

Similar threads

Back
Top