【问题标题】:Palindrome - Recursions in Python [duplicate]Palindrome - Python 中的递归
【发布时间】:2013-12-11 01:13:07
【问题描述】:

我正在编写 Python 代码,我必须在其中使用递归测试列表是否是回文,并且遇到了我的代码的混淆和问题:

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 false == thesublisttest.pop(0)
    elif len(thesublisttest) > 2:
        first = thesublisttest.pop(0)
        last = thesublisttest.pop()
        if first == last:
           return isPalindrome(thesublisttest)
        else:
         return False


def maxPalindrome( thelist ) :

        completelist=thelist[:]
        completelist.reverse()
        complete=len(thelist)-1

        for i in range(complete):
                if completelist[:]==thelist[:]:
                        x=len(thelist)
                        y=0
                        return(x,y)
                elif completelist[i:complete]==thelist[i:complete]:
                        successlist=thelist[i:complete]
                        a=i
                        b=len(thelist)-a
                        return (a,b)
        thelisttest = thelist[0:]
        if thelisttest:
                    return (0,0)


# test

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),

        [8,3,2,3],

        ]

for p in candidatePs :

    print p, isPalindrome( p )

    print p, "max", maxPalindrome( p )

我不确定我所做的是否被认为是递归,我也知道 [8,3,2,3] 应该显示 max(3,1) 并且我的代码将它吐出为 max (0,0) 我的代码中的任何帮助都会有很大帮助。

【问题讨论】:

  • 重复问题不使用递归来检查它是否是回文,但它仍然产生正确的输出。

标签: python recursion palindrome


【解决方案1】:
def max_palindrome(s, start_at):
    # recursion base, a list with only 1 item is a palindrome OR 
    # a list that's equals to its reverse
    if len(s) == 1 or s == s[::-1]:
        return (len(s), start_at)
    # if we got here the current list is not a palindrome, 
    # lets try to scan it by taking out one element from each of its sides
    return max(max_palindrome(s[1:], start_at+1), 
       max_palindrome(s[:-1], start_at))

for p in candidatePs :
    print p, "max", max_palindrome(p)

输出:

[1] max (1, 0)
[0, 1, 2, 3, 4, 5, 6, 7] max (1, 7)
[0, 1, 2, 3, 3, 2, 1, 0] max (8, 0)
[0, 1, 2, 3, 0, 3, 2, 1, 0] max (9, 0)
[0, 1, 2, 0, 1, 2, 3, 0, 3, 2, 1, 0] max (9, 3)
[8, 3, 2, 3] max (3, 1)

【讨论】:

  • 我很困惑这如何帮助我解决关于为什么 [8,3,2,3] 我得到最大 (0,0) 而不是最大 (3,1) 的问题。我的代码必须使用 def maxpalindrome(thelist) ,所以我看不出你写的内容如何帮助我解决我的问题。也许更多的解释会对我有所帮助。
  • 已更新以使用您的示例,如果有帮助请告诉我
  • 我还是有点困惑,因为在我的代码中回文 [8,3,2,3] 是候选 Ps 的一部分,所以我需要在我的 def maxpalindrome(thelist) 中包含代码我将如何将您写的内容添加到该部分。没有明确说明回文 = [8,3,2,3] 并打印 maxpalindrome
  • 在代码中添加了解释,我写的代码是完整的答案,不应该与其他(你的)代码集成
  • 更新为使用整个候选Ps集
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2012-11-24
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多