我已经写了一些代码,我希望能帮助我在列表中找到一个峰值[](我创建了一个列表,以便它总是在[-1]上有一个峰值只是为了测试一下这个算法是)。问题是这个错误不断出现,我不知道为什么。
Traceback (most recent call last):
File "C:\Users\Mageirakos\Desktop\PeakFinder.py", line 49, in <module>
devide(n)
File "C:\Users\Mageirakos\Desktop\PeakFinder.py", line 19, in devide
if a[int(n/2+0.5)]<a[int(n/2-1+0.5)]:
UnboundLocalError: local variable 'a' referenced before assignment
这是代码:
def devide(n):
if a[int(n/2+0.5)]<a[int(n/2-1+0.5)]:
a=a[:int(n/2)]
devide(n)
if len(a)==1:
print("PEAK:",a)
if a[int(n/2+0.5)]<a[int(n/2+1+0.5)]:
a=a[int(n/2):]
devide(n)
if len(a)==1:
print("PEAK:",a)
else:
print("PEAK:",a[n/2])
return
n = int(input("Input Size of List: "))
while n!=1:
a = []
s = 0
for i in range(n):
a.append(s)
s += 1
print("The devide & conquer method starts now")
devide(n)
print("and ends now")
n = int(input("Input Size of List: "))