在 PYTHON 中使用递归检查回文
Checking for palindrome using recursion in PYTHON
我正在检查一个词是否是回文,但我似乎无法让我的代码响应。我觉得它的声音很漂亮,但显然我错过了一些东西。有人可以指出那可能是什么吗?
def reverse(usrWrd, index):
newWord = ""
if index == len(usrWrd):
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1)
def main():
usrWrd = input("please enter a word to check for palindrome-ness:")
result = reverse(usrWrd, 0)
if result == usrWrd:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
main()
def isPalindrome(s):
length = len(s);
if length <= 1:
return True
if s[0] != s[length-1]:
return False
return isPalindrome(s[1:length-1])
或者对于那些喜欢更简洁代码的人:
def isPalindrome(s):
return (len(s) <= 1) or ((s[0] == s[-1]) and isPalindrome(s[1:-1]))
def reverse(usrWrd, index):
newWord = "" # This is a local
if index == len(usrWrd):
return newWord # It will always be empty string here
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1)
我完全不确定你为什么认为你需要 newWord
。你应该只比较第一个和最后一个字母是否相等,然后用你的递归函数检查剩下的子串也是回文。
如前所述,局部变量是问题所在,也许您可以一起发送局部变量。典型的尾递归实现
def reverse(usrWrd, index, newWord=''):
if index == len(usrWrd):
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1, newWord)
希望对您有所帮助!
def main():
usrWrd = input("please enter a word to check for palindrome-ness:")
result=reversed(usrWrd)
if list(result) == list(usrWrd):
print("That word is a palindrome")
else:
print("Word is NOT a palindrome")
# Correct approach to your solution
def reverse(usrWrd, index, newWord):
if index < 0:
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index - 1, newWord)
def main():
newWord = ""
usrWrd = input("please enter a word to check for palindrome-ness:")
result = reverse(usrWrd, len(usrWrd) - 1, newWord)
if result == usrWrd:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
################################################## ###########
# Alternate approach
def isPalindrome(usrWord):
sLen = len(usrWord)
if sLen <= 1:
return True
else:
if usrWord[0] != usrWord[sLen-1]:
return False
else:
return isPalindrome(usrWord[1:sLen-1])
def main():
newWord = ""
usrWrd = input("please enter a word to check for palindrome-ness:")
result = isPalindrome(usrWrd)
if result:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
################################################## ###########
# Pythonic way as suggested by 'Yaman Jain'
if usrWord == usrWord[::-1]:
return True # Palindrome
else:
return False # Not Palindrome
def rec_palindrome(S, n):
if not S:
return False
if n == 0:
return True
else:
return S[n] == S[(len(S)-1) - n] and rec_palindrome(S, n - 1)
如有改进请提出建议
def isPal(s):
if len(s) <= 1:
print("yes it is")
return True
else:
return s[0] == s[-1] and isPal(s[1:-1])
这是您可能想要检查的另一个解决方案!
string=string.lower()
if(len(string)<2):
return True
elif(string[0]!=string[-1]):
return False
else:
return string[1:-1]
palin=is_palindrome("Abbacus")
if(palin):
print("The string is palindrome")
else:
print("The string is not Palindrome")
我正在检查一个词是否是回文,但我似乎无法让我的代码响应。我觉得它的声音很漂亮,但显然我错过了一些东西。有人可以指出那可能是什么吗?
def reverse(usrWrd, index):
newWord = ""
if index == len(usrWrd):
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1)
def main():
usrWrd = input("please enter a word to check for palindrome-ness:")
result = reverse(usrWrd, 0)
if result == usrWrd:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
main()
def isPalindrome(s):
length = len(s);
if length <= 1:
return True
if s[0] != s[length-1]:
return False
return isPalindrome(s[1:length-1])
或者对于那些喜欢更简洁代码的人:
def isPalindrome(s):
return (len(s) <= 1) or ((s[0] == s[-1]) and isPalindrome(s[1:-1]))
def reverse(usrWrd, index):
newWord = "" # This is a local
if index == len(usrWrd):
return newWord # It will always be empty string here
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1)
我完全不确定你为什么认为你需要 newWord
。你应该只比较第一个和最后一个字母是否相等,然后用你的递归函数检查剩下的子串也是回文。
如前所述,局部变量是问题所在,也许您可以一起发送局部变量。典型的尾递归实现
def reverse(usrWrd, index, newWord=''):
if index == len(usrWrd):
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index + 1, newWord)
希望对您有所帮助!
def main():
usrWrd = input("please enter a word to check for palindrome-ness:")
result=reversed(usrWrd)
if list(result) == list(usrWrd):
print("That word is a palindrome")
else:
print("Word is NOT a palindrome")
# Correct approach to your solution
def reverse(usrWrd, index, newWord):
if index < 0:
return newWord
else:
newWord += usrWrd[index]
return reverse(usrWrd, index - 1, newWord)
def main():
newWord = ""
usrWrd = input("please enter a word to check for palindrome-ness:")
result = reverse(usrWrd, len(usrWrd) - 1, newWord)
if result == usrWrd:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
################################################## ###########
# Alternate approach
def isPalindrome(usrWord):
sLen = len(usrWord)
if sLen <= 1:
return True
else:
if usrWord[0] != usrWord[sLen-1]:
return False
else:
return isPalindrome(usrWord[1:sLen-1])
def main():
newWord = ""
usrWrd = input("please enter a word to check for palindrome-ness:")
result = isPalindrome(usrWrd)
if result:
print("That word is a palindrome")
else:
print("Sorry,",usrWrd, "is NOT a palindrome")
################################################## ###########
# Pythonic way as suggested by 'Yaman Jain'
if usrWord == usrWord[::-1]:
return True # Palindrome
else:
return False # Not Palindrome
def rec_palindrome(S, n):
if not S:
return False
if n == 0:
return True
else:
return S[n] == S[(len(S)-1) - n] and rec_palindrome(S, n - 1)
如有改进请提出建议
def isPal(s):
if len(s) <= 1:
print("yes it is")
return True
else:
return s[0] == s[-1] and isPal(s[1:-1])
这是您可能想要检查的另一个解决方案!
string=string.lower()
if(len(string)<2):
return True
elif(string[0]!=string[-1]):
return False
else:
return string[1:-1]
palin=is_palindrome("Abbacus")
if(palin):
print("The string is palindrome")
else:
print("The string is not Palindrome")