I am creating a multiplication table with all the elements in the array such as multiply all the elements(2,3,7,8,10) with first element(2). Then, multiply the new values(4,6,14,16,20) with second element(3) and so on.
I could achieve the result relatively simply with a loop.
def multiplication(arr):
newArr = arr.copy()
for j in range(len(arr)):
newArr= [val*arr[j] for val in newArr]
return newArr
print(multiplication([2,3,7,8,10]))
But when I try to solve it recursively, I am not able to retain the original elements with multiplied values.
def multiplicationRecursively(arr, index=0):
if (index >= len(arr)):
return arr
arr = [val*arr[index] for val in arr]
return multiplicationRecursively(arr, index+1)
print(multiplicationRecursively([2,3,7,8,10]))
Am I doing something wrong? I think I need to figure out how to retain the original array to get correct multiplier ? I'm learning the Recursive Programming so I'm not willing to use the probably built-in methods.