稍微進階的python開發者這時候不會想要遞迴,會想yield。
import numpy as np
a = np.random.randn(100)*0.02
def compound(n, arr):
if n <= 0 or n > len(a):
return None
tmp = arr[0] + 1
for i in range(0, n):
if i == 0:
yield arr[0]
else:
tmp *= arr[i] + 1
yield tmp - 1
print(list(compound(10, a)))
https://pyfiddle.io/fiddle/86943b25-0db7-4fb5-b13f-987c1006fbd2/?i=true
複利嗎?
不知道Python能不能跑遞迴?