Longest Palindrome Substring : Index Error ( list index out of range)

43 Views Asked by At

Index Error , List index out of range

In "if" statement, the second line gives the above specific error

You can see the value of index(ind) being used for the list(temp). It is clearly not out of range but it still gives the error.

def longestPalindrome(s):
    n = 0
    e = 1
    temp = []
    l = []
    x = []
    length = []
    ind = 1
    
    
    for i in range(1,len(s)+1):
        temp.append(i)
        
    
    while [] not in x:
        l = s[n:e]
        
        if n == len(s):
            n=0
            e = temp[ind]
            ind = ind+1
            l = s[n:e]
            if l[::1] == l[::-1]:
                x.append(l)
                length.append(len(l))
        
        elif l[::1] == l[::-1]:
            x.append(l)
            length.append(len(l))
            
        
        n = n+1     
        e = e+1
    return x[length.index(max(length))]
1

There are 1 best solutions below

0
On

because index starts in zero.. when e = temp[ind] gets the lenght of the word is asking for a index longer than your word