我一直使第4行的字符串索引超出范围(如果s [i]!= s [end-i] :),但我不知道为什么。我在Visualizer上运行了代码,但这并没有帮助我。 在输入字符串s后,该代码应该给我最长的回文子字符串。
这是我的代码:
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
答案 0 :(得分:1)
Python range(start, end)
为您提供了一个从start
到end-1
(含)的数字序列。例如,range(10, 14)
是10、11、12、13。现在考虑以下循环:
for i in range (start,end):
if s[i] != s[end-i]:
isPal = False
如果使用我的if
示例,则range(10, 14)
部分将s[10] != s[14-10]
与s[13] != s[14-13]
进行比较。显然不是您打算要做的。
可能是这个意思:
for i in range(end-start):
if s[start+i] != s[end-i]:
isPal = False
要解决您的问题,可以使用(长)单层衬套:
print(max([s[i:j+1] for i in range(len(s)) for j in range(i+1, len(s)) if s[i:j+1] == "".join(reversed(s[i:j+1]))], key=lambda x: len(x)))
答案 1 :(得分:1)
您的代码存在多个问题,包括检查回文的逻辑。
在longestPalSubsB函数中,变量'i'的范围为0到len(s)-1。
range(n)
的范围是0到n-1
。因此,应使用for i in range(len(s)):
语法。类似的语法适用于j
。
假设str = "STARWARS"
,str[0:1]
返回'S'
,而str[1:4]
返回“ TAR”。
return s[start,end]
引发语法错误。应该是s[start:end+1]
在isSubPalindrome
函数中,应使用变量i
比较最后一个和第一个字符,第二个最后一个和第二个字符,依此类推。下面的代码应该可以解决问题。
def isSubPalindrome (s):
isPal = True
n = len(s)
mid = int(n/2)
for i in range(mid):
if s[i] != s[n-1-i]:
isPal = False
return isPal
在longestPalSubsB
函数中不需要k的第三循环。使用st = s[i:j+1]
,isSubPalindrome (st)
您可能想使用交互式python shell检查语法,该shell可以通过在终端或cmd中键入python
来生成。 python -i test.py
提供了一个交互式python shell,其中包含代码的可变值。快乐的Python编码!
答案 2 :(得分:0)
这是工作代码:)
def isSubPalindrome (s,start,end):
isPal = True
st = s[start:end]
mid = int(len(st)/2)
n = len(st)
for i in range (mid):
if st[i] != st[n-1-i]:
isPal = False
return isPal
def longestPalSubsB (s):
MaxLen = 0
start = 0
end = 0
for i in range (len(s)):
for j in range (i,len(s)):
st = []
Len = 0
for k in range (i,j+1):
st.append(s[k])
Len = len (st)
if Len > MaxLen and isSubPalindrome (s,i,j+1):
MaxLen = Len
start = i
end = j + 1
return s[start:end]
s = input("Enter a string: ")
print (longestPalSubsB(s))