检测上升趋势和下降趋势

我正在做一个项目,并试图创建一个函数来检测趋势的增长或下降,因为一次只能给它一个元素,因为我一开始无法访问所有值。

就像这样:

第一次迭代:

def detect_trend(arr[0], k, r, q):
       return array_upward_trend, array_downward_trend

第二次迭代:

def detect_trend(arr[1], k, r, q):
       return array_upward_trend, array_downward_trend

等等。

The k is what defines a trend. Meaning that a trend is only formed if it has increased/decreased the last k elements. Although if a trend has started to form and after q elements we give the function the value arr[i], if this value is either a duplicate or an element within a small range r from the previous element arr[i-1] given to the function, then it does not disrupt the creation of the trend.

例如,我有这个数组

arr=[72 92 42 130 131 412 412 512 345 301 257 101 101 65 72 87 89 80 76 76 75 72 73]

the output after len(arr) iterations would be

array_upward_trend=[[42 130 131 412 412 512],[65 72 87 89]]
array_downward_trend=[[512 345 301 257 101 101 65], [89 80 76 76 75 72 73]]

k=4, q=3 and r=0.015 ( r in this case means a range of 1.5 %) for the example

我无法完成这项工作,并且再次从头开始。我真的很感谢任何帮助

谢谢