If the str is a palindrome, return True; otherwise, return False. Punctuation, spaces, and other non-letters are ignored; their presence or absence should have no effect on the result. Uppercase letters are considered to be equal to their lowercase equivalents.

So my question basically is... How do I "ignore" the punctuation, spaces and other non-letters? I know how to check for the palindrome but yeah... Anyways, thanks =].
So what I had before was just a check for palindrome was this:

def is_palindrome(list):
    i = 0
    true_or_false = True
    while i <= len(list) -1 and true_or_false == True:
        if list[i] == list [-1-i]:
            true_or_false =  True
        else:
            true_or_false = False
        i +=1
    return true_or_false

So what can I do to "ignore" the punctuations and stuff?

I kinda know how to write it in a for loop... don't know how to change it to a while loop xD

try to add this in your program and it should work

def remove_non_words( s ):
    new_s = ""
    for l in s.lower():
        if l in "abcdefghijklmnopqrstuvwxyz":
            new_s += l
            
    return new_s

it returns a new string with the punctuation removed :)

:: is wondering where to put it :: XD

oh and, i'm trying to do it with while loops, not for loops.

Edited 7 Years Ago by Kolz: n/a

well you can simply convert this into a while loop like this:

def remove_non_words( s ):
    new_s = ""
    index = 0
    s = s.lower()
    while index < len( s ):
        if s[ index ] in "abcdefghijklmnopqrstuvwxyz":
            new_s += s[ index ]
        index += 1
 
    return new_s

def is_palindrome( s ):
    i = 0
    s = remove_non_words( s )
    true_or_false = True
    while i <= len( s ) -1 and true_or_false == True:
        if s[i] == s[ -1 -i ]:
            true_or_false =  True
        else:
            true_or_false = False
        i +=1
    return true_or_false

print is_palindrome( "r,.,';  acecar" )

I thinks it's not a good idea to use 'list' as the parameter name in the is_palindrome function. Use a different one :) hope this helps :)

This article has been dead for over six months. Start a new discussion instead.