So I have the following:
class Tree(object):
def__init__(self):
self.right = None
self.left = None
self.val = None
I populate the tree with stuff in my code.
I'd like to write a function of the form
def mult(newVal, node)
newVal = diff(node.left, newVal, var)
newVal.append('*')
newval.append(next(node.right))
newVal.append('+')
newVal = diff(node.left, newVal, var)
newVal.append('*')
newVal.append(next(node.left))
Where next is simply a function that traverses to the next node in the tree, and diff is a recursive function of the following form:
def diff(node, newVal, var):
...
...
elif(node.val == '*'):
newVal = diff(node.left, newVal, var)
newVal.append('*')
newval.append(next(node.right))
newVal.append('+')
newVal = diff(node.left, newVal, var)
newVal.append('*')
newVal.append(next(node.left))
...
...
and my "main" is
node = Tree()
newEquation = []
pos = 0
pos, newTree = buildTree(node, equation, pos)
newEquation = diff(newTree, newEquation, variable)
newEquation = ''.join(newEquation)
print newEquation
How would I write the def mult() function to accept a tree node as a parameter, and then call the diff() function again?
-
What's wrong with how you've written it?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2012年04月14日 02:37:18 +00:00Commented Apr 14, 2012 at 2:37
-
it's fixed, a syntax error was throwing a strange error message. Thanks!gfppaste– gfppaste2012年04月14日 02:43:14 +00:00Commented Apr 14, 2012 at 2:43
1 Answer 1
Python is not typed, so if you call mult with:
mult(newEquation, node)
That should work just fine.
answered Apr 14, 2012 at 2:45
Charles Menguy
41.6k18 gold badges97 silver badges117 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py