In what case is recursion more efficient?
I'm curious as to know how exactly recursion is more efficient. For functions like these:
Given a list A = [1,2,3,4,5] and B = [5,4,3,2,1], call vector_dot and result in int(1*5 + 2* 4 + 3*3...etc).
python Code:
def vector_dot(a, b):
if len(a) == 1:
return a[0] * b[0]
else:
return (a[0] * b[0]) + vector_dot(a[1:],b[1:])
def vector_dot(a,b):
result = 0
for x in range(len(a)): #obv a and b have to be same length
result += a[x] * b[x]
return result
does it really matter what you use? You'll end up getting the same answer, as well as it being the same amount of lines.