0

I have writen down some code which I hope helps me find a peak in a list a[] (I created the list in a way so that it always has the peak on a[-1] just to test out how fast this algorithm is).The problem is that this error keeps popping up and I dont know why.

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

This is the code :

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: "))

0 Answers0