Python 中的递归函数回文 [英] Recursive Function palindrome in Python

查看:33
本文介绍了Python 中的递归函数回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助编写一个递归函数来检测字符串是否为回文.但我不能使用任何循环,它必须是递归的.谁能帮我看看这是怎么做的.我使用的是 Python.

I need help writing a recursive function which detects whether a string is a palindrome. But i can't use any loops it must be recursive. Can anyone help show me how this is done . Im using Python.

推荐答案

def ispalindrome(word):
    if len(word) < 2: return True
    if word[0] != word[-1]: return False
    return ispalindrome(word[1:-1])

这是最好的一个班轮

def ispalindrome(word):
    return word == word[::-1]

这篇关于Python 中的递归函数回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆