Given a list of numbers, I'm trying to write code that finds the difference between consecutive elements. For example, A = [1, 10, 100, 50, 40]therefore, the output of the function should be [0, 9, 90, 50, 10]. Here is what I have been trying to use recursion so far:
def deviation(A):
if len(A) < 2:
return
else:
return [abs(A[0]-A[1])] + [deviation(A[1: ])]
However, the output I get (using Example A above as an input) is [9, [90, [50, [10, None]]]]. How to format brackets correctly? (I tried to guess and check, but Iβm the closest I got) And how to write it, where it subtracts the current element from the previous element without receiving an index error for the first element? I still want the first element of the output list to be null, but I donβt know how to do this using recursion and for some reason seems best to me.
source
share