+ 1
How many Iteration and Recursive calls does it take to calculate the sum of numbers in a list of size n?
Say you have a_list = [2,3,4,8,6]
1 Answer
+ 2
One interation.
Recursive func would be like this:
def CalcSum(arr,sum,index) :
if(index!=len(arr)):
return CalcSum(arr,sum+arr[index],index+1)
return sum
A loop function would look like this.
def CalcSum(arr):
sum=0
for x in range(len(arr)):
sum+=arr[x]
return sum