10

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

4

11 回答 11

67
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]
于 2009-06-04T19:32:06.923 回答
46

从一般算法的角度来看,递归函数有3种情况:

1)还剩 0 件。项目是回文,通过身份。

2)还剩 1 件。项目是回文,通过身份。

3) 2 个或更多项目。删除第一个和最后一个项目。相比。如果它们相同,则在字符串的左侧调用函数。如果 first 和 last 不相同,则 item 不是回文

函数本身的实现留给读者作为练习:)

于 2009-06-04T17:55:26.463 回答
3

由于我们无论如何都在发布代码,并且还没有发布任何一条线,所以这里是:

def palindrome(s):
    return len(s) < 2 or s[0] == s[-1] and palindrome(s[1:-1])
于 2009-06-04T19:41:57.807 回答
3

如果字符串的长度为零或一个字母,则它是回文。

如果一个字符串的第一个和最后一个字母相同,而剩余的字母(我认为它是 Python 中的一个[1: -1]切片,但我的 Python 有点生疏)是回文,那就是回文。

现在,把它写成一个接受字符串的回文函数。它会调用自己。

于 2009-06-04T17:55:42.173 回答
2

这是另一个观点

回文字符串是

  1. 某个字母x

  2. 一些回文子串。

  3. 重复相同的字母x

另外,请注意,您可能会得到一个正确的英文句子“Able was I ere I saw Elba”。用标点符号。您的回文检查器可能不得不悄悄地跳过标点符号。此外,您可能不得不在不考虑大小写的情况下悄悄匹配。这稍微复杂一些。

  1. 一些领先的标点符号。某个字母x

  2. 一些回文子串。

  3. 某些字母x不分大小写地重复。一些尾随标点符号。

而且,根据定义,零长度字符串是回文。单字母字符串(删除标点符号后)也是回文。

于 2009-06-04T18:03:40.740 回答
2

这是您可以想到简单递归函数的一种方式...翻转问题并以这种方式思考。你如何递归地制作回文?这就是我将如何做到的......

def make_palindrome():
    maybe:
        return ""
    elsemaybe:
        return some_char()
    else:
        c = some_char()
        return c + make_palindrome() + c

然后你可以翻转它来构建测试。

于 2009-06-04T18:16:59.547 回答
1

该函数应该期望一个字符串。如果字符串中有多个字母,则比较第一个和最后一个字母。如果是 1 或 0 个字母,则返回 true。如果两个字母相等,则再次使用字符串调用函数,不带第一个和最后一个字母。如果它们不相等,则返回 false。

 palindrom( word):
   IF length of word 1 or 0 THEN
      return 0;
   IF last and first letter equal THEN
     word := remove first and last letter of word;
     palindrom( word);
   ELSE
     return false;
于 2009-06-04T18:04:03.860 回答
0
a=raw_input("enter the string:")
b=len(a)
c=0
for i in range(b):
    if a[i]==a[-(i+1)]:
        c=c+1
if c==b:
    print a,"is polindrome"
else:
    print a,"is not polindrome"
于 2011-03-29T10:36:38.673 回答
0

我的解决方案

#To solve this I'm using the stride notation within a slice [::]
def amazonPalindrome(input):
    inputB = input
    input = input[::-1]
    #print input
    noPalindrome = inputB + " is not a palindrome"
    isPalindrome = inputB + " is a palindrome"
    #compare the value of the reversed string to input string
    if input[0]!= input[-1]: 
        print noPalindrome
    else:
        print isPalindrome


#invoking the def requires at least 1 value or else it fails
#tests include splitting the string,mixing integers, odd amount palindromes.
#call the def  
amazonPalindrome('yayay')
于 2011-04-29T07:40:49.120 回答
-1

这是C版本,如果有人碰巧登陆这里搜索C代码!

int IsPalindrome_Recursive(char *s, int start, int end)
{
    if ((end - start) < 2)
    {
        return 1;
    }
    if (s[start] != s[end])
    {
        return 0;
    }
    return IsPalindrome_Recursive(s, ++start, --end);
}

调用为:

IsPalindrome_Recursive(s, 0, strlen(s) - 1)
于 2011-10-22T04:21:14.123 回答
-1
n=raw_input("Enter a number===>")
n=str(n)
l=len(n)
s=""
for i in range(1,l+1):
    s=s+n[l-i]
if s==n:
    print "Given number is polindrom"
else:
    print "Given number is not polindrom"
于 2010-08-10T09:47:26.530 回答