python中回文设计_Python中的回文递归

我正在编写一个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)

任何对我的代码的帮助都会有很大的帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值