Palindrome

Alan Kennedy alanmk at hotmail.com
Fri Nov 14 09:34:23 EST 2003


Ron Adam wrote:
> I notice right after I posted it,  I can simplify the test function a
> bit more.
> 
> import re
> def palindrome_test(p):
>     p = p.lower()
>     p = re.sub(r'\W','',p)
>     while p and p[:1] == p[-1:]:
>             p = p[1:-1]
>     return (len(p) <= 1)

Ron,

If I were going to follow this approach, I would try to eliminate any
copying, like so:

def palindrome_test(p):
    p = p.lower()
    p = re.sub(r'\W','',p)
    plen = len(p)
    for i in xrange(plen/2):
        if p[i] != p[plen-i-1]:
            return False
    return True

regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list