每当我尝试运行此代码时:
def isPalindrome( theSubList ):
theSubListtest = theSubList[0:]
if len(theSubListtest) <= 1:
return True
elif len(theSubListtest) == 2:
x = theSubListtest[0]
y = theSubListtest[1]
if (x == y):
return True
else:
return Falsefirst == theSubListtest.pop(0)
elif len(theSubListtest) > 2:
first = theSubListtest.pop(0)
last = theSubListtest.pop()
if first == last:
isPalindrome(theSubListtest)
else:
return False
candidatePs = [
[1,],
range(8),
range(4) + range(3,-1,-1),
range(4) + [0] + range(3,-1,-1),
range(3) + range(4) + [0] + range(3,-1,-1),
]
for p in candidatePs :
print p, isPalindrome( p )
它为p的前两个值正确运行,但是对于以下三个值输出“None”。任何帮助是极大的赞赏。提前谢谢。
答案 0 :(得分:4)
糟糕。
if (first == last):
return isPalindrome(theSubListtest)
else:
return False
答案 1 :(得分:1)
你忘记了回归。改变这些行:
if (first == last):
isPalindrome(theSubListtest)
到
if (first == last):
return isPalindrome(theSubListtest)
并且代码将按预期工作。