palindrome iteration

Aahz aahz at pythoncraft.com
Sat Sep 11 17:03:58 EDT 2010


In article <mailman.103.1282914852.29448.python-list at python.org>,
Dave Angel  <davea at ieee.org> wrote:
>
>def is_palindrom(s):
>    s = s.lower()
>    return s == s[::-1]

To deal with "real" palindromes such as, "Madam, I'm Adam," you should
probably strip all spaces and punctuation:

# untested
pat = re.compile(r'[a-z]')
def is_palindrome(s):
    letters = pat.findall(s.lower())
    return letters == reversed(letters)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

[on old computer technologies and programmers]  "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As."  --Andrew Dalke



More information about the Python-list mailing list