0

每当我尝试运行此代码时:

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 的前两个值正确运行,但随后为以下三个值输出“无”。任何帮助是极大的赞赏。提前致谢。

4

2 回答 2

4

哎呀。

if (first == last):
    return isPalindrome(theSubListtest)
else:
    return False
于 2011-10-23T23:38:52.303 回答
1

你忘记退货了。更改这些行:

if (first == last):
    isPalindrome(theSubListtest)

if (first == last):
    return isPalindrome(theSubListtest)

并且代码将按预期工作。

于 2011-10-23T23:43:23.237 回答