Python中的递归函数回文
我需要帮助编写一个递归函数来检测字符串是否是回文。但我不能使用任何循环,它必须是递归的。谁能告诉我这是怎么做的。我正在使用Python。从一般算法的角度来看,递归函数有3种情况: 1) 剩下0个项目。项目是一个标识 2) 左1项。项目是一个标识 3) 两个或多个项目。删除第一个和最后一个项目。比较一下。如果它们相同,则调用字符串左边的函数。如果第一个和最后一个不相同,则项目不是一个Python中的递归函数回文,python,recursion,palindrome,Python,Recursion,Palindrome,我需要帮助编写一个递归函数来检测字符串是否是回文。但我不能使用任何循环,它必须是递归的。谁能告诉我这是怎么做的。我正在使用Python。从一般算法的角度来看,递归函数有3种情况: 1) 剩下0个项目。项目是一个标识 2) 左1项。项目是一个标识 3) 两个或多个项目。删除第一个和最后一个项目。比较一下。如果它们相同,则调用字符串左边的函数。如果第一个和最后一个不相同,则项目不是一个 函数本身的实现留给读者作为练习:)如果字符串长度为零或一个字母,那么它就是回文 如果一个字符串的首字母和末字母相同
函数本身的实现留给读者作为练习:)如果字符串长度为零或一个字母,那么它就是回文 如果一个字符串的首字母和末字母相同,而其余的字母(我认为这是Python中的
[1:-1]
片段,但我的Python有点生疏)是回文,那么它就是回文
现在,把它写成一个回文函数,它接受一个字符串。它会称自己为。这里是另一个观点 回文字符串是
根据定义,零长度字符串是回文。此外,单个字母字符串(删除标点符号后)也是回文。函数应使用字符串。如果字符串中有多个字母,请比较第一个字母和最后一个字母。如果为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;
这里有一种方法可以让你想到简单的递归函数。。。把问题翻过来,这样想。如何递归地生成回文?我会这样做的
def make_palindrome():
maybe:
return ""
elsemaybe:
return some_char()
else:
c = some_char()
return c + make_palindrome() + c
然后您可以翻转它来构建测试。因为我们无论如何都在发布代码,而且还没有发布任何一行代码,所以如下所示:
def ispalindrome(word):
if len(word) < 2: return True
if word[0] != word[-1]: return False
return ispalindrome(word[1:-1])
def palindrome(s):
return len(s) < 2 or s[0] == s[-1] and palindrome(s[1:-1])
def回文:
返回len(s)<2或s[0]==s[-1]和回文(s[1:-1])
我的解决方案
#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')
这里是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);
}
-1:为某人的家庭作业张贴代码——你真丢脸。如果这是真正的家庭作业,我会同意你的看法,但事实并非如此。提问者正在学习期中考试。仅仅记住这个答案而不是试图理解它对他没有帮助:当然在考试中他需要解决一个不同的递归问题。我看不出它有多递归。@billwild它大约是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"
#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')
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)