-1

I'm working on a solution for finding a valid palindrome, where an empty string would be considered a valid palindrome. This question ignores any cases which are not alphanumeric. Following are some examples:

Input: "A man, a plan, a canal: Panama"
Output: true

Input: "race a car"
Output: false

I wrote a solution where if I encounter a non-alphanumeric character, that character would be skipped:

def isPalindrome(self, s):
1        """
2        :type s: str
3        :rtype: bool
4        """
5        """
6        for i in s:
7            if (i.isupper())==True:
8                i.lower()
9        """
10        if len(s)==0:
11            return True
12        print(s)

13        i = 0
14        j = len(s) - 1

15        while(i<=j):  
16            if s[i].isalnum()==False:
17                i+=1
18            elif s[j].isalnum() == False:
19                j-=1
20            elif s[i].islower() != s[j].islower():
21                return False
22            i+=1
23            j-=1
24            
25        return True

However I keep on getting the following result:

Your input
"A man, a plan, a canal: Panama"
stdout
A man, a plan, a canal: Panama


Output
false

Expected
true

What am I doing wrong?

  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Your posted code simply defines a function (intended as a method?) and quits. You show no effort to trace the problem. – Prune Oct 09 '19 at 00:14
  • 1
    See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. If nothing else, some strategic `print` statements will trace your control and data flow. – Prune Oct 09 '19 at 00:15

1 Answers1

0

The first problem jumped out at me as I was about to close this tab:

        elif s[i].islower() != s[j].islower():

This is not the comparison you need. islower returns whether or not the letter is lower case. This will fail on your first attempt, comparing 'A'.islower() (which is False) against 'a'.islower() (which is True). The comparison you need is

        elif s[i].lower() != s[j].lower():

i.e. drop both letters to lower-case and compare them.

There are more problems in your index handling, but this -- and a few print commands -- should get you moving.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • you're right. I made this change in my program. However I'm still getting an error, namely 'IndexError: string out of range'. on line 16: 16 if s[i].isalnum()==False: – boomselector Oct 09 '19 at 20:15
  • i fixed the indexing, thanks for your help. instead of using the if and elif statements, I've used while statements: while (i – boomselector Oct 09 '19 at 22:39