0

Is it possible to turn this program I have written into a single recursion function that ignores case differences, white spaces, and non-English letters? For example, the program must be able to return "True" to the following string, ignoring the uppercase "R" and the period."Rats live on no evil star.". No imports, only one function and the only the method that can be used is isalpha().

    def main() :

             inputStr = input("Enter a string: ")

             if isPalindrome(inputStr) :

                       print("That's a palindrome.")

             else:
                       print("That isn't a palindrome.")


   def isPalindrome(string) :

         if len(string) <= 1 :
               return True

         if string[0].lower() == string[len(string) - 1].lower() :
               return isPalindrome(string[1:len(string) - 1])

         else :
               return False

   main()
Tony Stark
  • 71
  • 1
  • 2
  • 9

3 Answers3

1

Preprocess your string first, keeping only alphas as lowercases and spaces.

string = "".join(char.lower() for char in string if char.isalpha())

If you do this first, your function works for me and returns True with your input.


Final function :

def is_palindrome(string) :

    string = "".join(char.lower() for char in string if char.isalpha())

    if len(string) <= 1 :
        return True

    if string[0] == string[-1]:
        return is_palindrome(string[1:-1])

    else :
        return False

Now

>>> is_palindrome("Rats live on no evil star.")
True
arnaud
  • 3,293
  • 1
  • 10
  • 27
  • 1
    Is it possible to write the program using isalpha() and no other methods like lower()? – Tony Stark Feb 18 '18 at 00:02
  • I cleaned the code a little. For now, not sure how we can avoid the use of `lower()` though. – arnaud Feb 18 '18 at 00:11
  • 1
    You can downcase without string.lower() by testing if your character is between A and Z, and then subtracting ord('A') and adding ord('a'). That's the way it used to be done. – dstromberg Feb 18 '18 at 01:11
  • While it does answer the question, you should be aware that, other than the initial mod to the string in the first recursion level, every other level does it unnecessarily. – paxdiablo Feb 18 '18 at 01:40
0

Since ord() and len() are functions, not methods of str, this should do the trick:

def isPalindrome(string):

    a, b = 0, len(string)

    if b <= 1:
        return True

    while not string[0].isalpha():
        a += 1
        if len(string[a:b]) <= 1:
            return True

    while not string[b - 1].isalpha():
        b -= 1
        if len(string[a:b]) <= 1:
            return True

    if (ord(string[a]) - ord(string[b - 1])) % 32 == 0:
        return isPalindrome(string[a + 1:b - 1])

    return False

print(isPalindrome("Rats live on no evil star."))
print(isPalindrome("rats live on no evil star."))
print(isPalindrome("rats live on no evil star"))

print(isPalindrome("Madam, I'm Adam."))
cdlane
  • 40,441
  • 5
  • 32
  • 81
-1

> For non-alphabetical characters (except for digits) Suppose s is your string.

from string import punctuation
St=(s.strip(punctuation))