python: recursive check to determine whether string is a palindrome

前端 未结 6 1319
死守一世寂寞
死守一世寂寞 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
    

提交回复
热议问题