i have a function its name is positive_negative
def positive_negative(list_changes):
""" (list of number) -> (number, number) tuple
list_changes contains a list of float numbers. Return a 2-item
tuple where the first item is the sum of the positive numbers in list_changes and
the second is the sum of the negative numbers in list_changes.
>>> positive_negative([0.01, 0.03, -0.02, -0.14, 0, 0, 0.10, -0.01])
(0.14, -0.17)
"""
i can write this function using list techniques as follow :
def positive_negative(list_changes):
pos = sum([item for item in list_changes if item > 0.0])
neg = sum ([item for item in list_changes if item < 0.0])
return pos, neg
and that is a good solution. now my question is how to use the recursion techniques to solve the same function , i have tried the following code , but unfortunately there is something wrong .
def positive_negative(list_changes):
pos = 0.0
neg = 0.0
if len(list_changes)== 0:
pos =+ 0.0
neg =+ 0.0
return pos,neg
else:
if list_changes[0] > 0.0 :
pos =+ list_changes[0]
else:
neg =+ list_changes[0]
positive_negative(list_changes[1:])
return pos,neg
can you help me find what is my mistake and how to get the right recursive function.
thank you
sum, it builds it lazily.