I keep getting string index out of range on line 4 (if s[i] != s[end-i]:) but I can't figure out why. I ran my code on visualizer but that didn't help me. The code is supposed to give me the longest palindromic substring after I input a string s
here is my code:
def isSubPalindrome (s,start,end):
isPal = True
for i in range (start,end):
if s[i] != s[end-i]:
isPal = False
return isPal
def longestPalSubsB (s):
MaxLen = 0
for i in range (len(s)-1):
for j in range (i,len(s)-1):
st = ""
for k in range (i,j):
st = st + s[k]
if isSubPalindrome (st,i,j) == True and len(st)>MaxLen:
MaxLen = len (st)
start = i
end = j
return s[start,end]
s = input("Enter a string: ")
print (longestPalSubsB(s))
Enter a string: aceexcivicgrfdds
Traceback (most recent call last):
File "<ipython-input-6-64661b5bf324>", line 1, in <module>
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 4/Problem2b.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 4')
File "/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 4/Problem2b.py", line 35, in <module>
print (longestPalSubsB(s))
File "/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 4/Problem2b.py", line 24, in longestPalSubsB
if isSubPalindrome (st,i,j) == True and len(st)>MaxLen:
File "/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /HW/Homework 4/Problem2b.py", line 12, in isSubPalindrome
if s[i] != s[end+1-i]:
IndexError: string index out of range
sis just a single character