2017-04-27 161 views
0

我正在检查一个单词是否是回文,而且我似乎无法让我的代码响应。我觉得它很漂亮,但显然我错过了一些东西。有人能指出可能是什么吗?在PYTHON中使用递归检查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() 
+0

要设置'newWord'到空字符串每次在'reverse()' – kuro

+0

也用'len(usrWrd) - 1'而不是'0'调用'reverse' – kuro

+1

pythonic方式 's == s [:: - 1]'如果s是palidrome海峡否则为假 –

回答

0
# 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 
0
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])) 
+1

您可以使用'-1'代替'length - 1' –

+0

确实如此。但我认为,由于op是编程方面的新手,我故意使用更加冗长的风格来提高可读性。 – selbie

+0

Meh ..看起来像golang –

0
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在所有。你应该只比较第一个和最后一个字母是否相等,然后使用递归函数检查剩余的子字符串是否也是回文。

0

如上所述局部变量是问题,可能是你可以发送局部变量。典型的尾递归实现

def reverse(usrWrd, index, newWord=''): 

    if index == len(usrWrd): 
     return newWord  
    else: 
     newWord += usrWrd[index] 
     return reverse(usrWrd, index + 1, newWord) 

希望它有帮助!

0
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")