The function is missing an else statement to increment the value of i. This causes an infinite loop and the function never reaches the return statement. Additionally, the condition in the while loop needs to be adjusted to i<(len(array)-2) to prevent an index out of range error.

Here is the corrected code:

def peakFinder_1(array):
    array.append(-10)
    i = 0
    while i < (len(array) - 2):
        if array[i] >= array[i - 1] and array[i] >= array[i + 1]:
            return array[i]
        else:
            i += 1

Now the function should return the first peak it finds in the array.

def peakFinder_1array arrayappend-10 i=0; while ilenarray-1 if arrayi=arrayi-1 and arrayi=arrayi+1 return arrayi i+=1程序无输出

原文地址: http://www.cveoy.top/t/topic/i7zk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录