python: recursive check to determine whether string is a palindrome

前端 未结 6 1320
死守一世寂寞
死守一世寂寞 2021-01-19 17:11

My task is to define a procedure is_palindrome, that takes as input a string, and returns a boolean indicating if the input string is a palindrome. In this case a single let

相关标签:
6条回答
  • 2021-01-19 17:22

    In your first example, you forgot a return statement:

    def is_palindrome(s):
        if s == '':
            return True
        else:
            if (ord(s[0]) - ord(s[len(s)-1])) == 0:
                # v-- forgot this here
                return is_palindrome(s[1:len(s)-1])
            else:
                return False
    
    0 讨论(0)
  • 2021-01-19 17:23
            is_palindrome(s[1:len(s)-1])
    

    needs to be...

            return is_palindrome(s[1:len(s)-1])
    

    in your first version, or

            result = is_palindrome(s[1:len(s)-1])
    

    in your second. Otherwise, you never actually propagate the recursive call's return value back to the original caller.

    0 讨论(0)
  • 2021-01-19 17:26
    def is_palindrome(s):
        if not s:
            return True
        else:
            return s[0]==s[-1] and is_palindrome(s[1:-1])
    

    or, if you want a one-liner:

    def is_palindrome(s):
        return (not s) or (s[0]==s[-1] and is_palindrome(s[1:-1]))
    

    Hope that helps

    0 讨论(0)
  • 2021-01-19 17:28

    Respuesta en Java

    public class Varios {
    
    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
        System.out.println( pali("anitalavalatina"));
      }
      static boolean pali(String palabra){
        System.out.println(palabra);
        if (palabra.length()-1<2) 
                return true;
        if(palabra.charAt(0)!=palabra.charAt(palabra.length()-1)) return false;
        return pali(palabra.substring(1,palabra.length()-1));
      }
    
    }
    
    0 讨论(0)
  • 2021-01-19 17:32
    # ask user to enter any string
    a = raw_input("Enter the string : ")
    #palindrome check
    print (a == a[::-1]) and "String is palindrome" or "String is not palindrome"
    
    0 讨论(0)
  • 2021-01-19 17:37

    Let's step through your second example, line by line.:

    def is_palindrome(s):
    

    In this case let's let s = "abba", which is the first string you got an error on:

            if s == '':
    

    is evaluated as

            if 'abba' == '':
    

    Which is False, so we skip ahead to else:

            else:
                if (ord(s[0]) - ord(s[len(s)-1])) == 0:
    

    This if statement is equivalent to:

                if (97 - 97) == 0:
    

    It's True, so recursion happens:

                    is_palindrome(s[1:len(s)-1])
    

    or

                    is_palindrome('bb')
    

    Now whatever is the result of this recursion, we ignore it, because the return value is not saved. Thus, when we get to this line:

            return result
    

    We never defined what result was, so Python flips out.

    Other posters already did an excellent job of answering your question. I'm posting to demonstrate the importance of tracing a program to find/fix bugs.

    0 讨论(0)
提交回复
热议问题