2017-04-27 165 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")