![]() |
Re: Palindrome
To remove the characters that are not alphanumeric I would have used
filter : t=filter(lambda x: x.isalnum(),list(s.lower())) Pierre --- In python-list@yahoogroups.com, runic911@a... (Runic911) wrote: > Does anyone know how i can fix my Palindrome program? > > s = raw_input('Enter a String: ') > punctuation = '%$!*.,-:? ;()\'\"\\' > i = 0 > h = 0 > t = 0 > p = '' > z = 0 > while s!= ' ': > while i <= len(s) - 1: > punct = s[i] > if punctuation.find(punct) == -1: > p = p + punct > i = i + 1 > t = p.lower() > t[h] == t[len(t)-1-h] > -- > http://mail.python.org/mailman/listinfo/python-list |
Re: Palindrome
Pierre Quentel wrote:
> To remove the characters that are not alphanumeric I would have used > filter : > > t=filter(lambda x: x.isalnum(),list(s.lower())) or perhaps more clearly...: t = [ c for c in s.lower() if c.isalnum() ] Then, "list t comes from a palindromic string s" can be coded as: return t == t[::-1] (in Python 2.3). Alex |
Re: Palindrome
"Pierre Quentel" <quentel.pierre@wanadoo.fr> wrote in message
news:mailman.715.1068739302.702.python-list@python.org... > To remove the characters that are not alphanumeric I would have used > filter : > > t=filter(lambda x: x.isalnum(),list(s.lower())) Why not the more readable list comprehension? t = [c for c in s.lower() if c.isalnum()] It's even a tiny bit faster: ....>python lib/timeit.py -s"s='Go Hang a Salami! I\'m a Lasagna Hog!'" "t=filter(lambda x: x.isalnum(),list(s.lower()))" 10000 loops, best of 3: 91.6 usec per loop ....>python lib/timeit.py -s"s='Go Hang a Salami! I\'m a Lasagna Hog!'" "t = [c for c in s.lower() if c.isalnum()]" 10000 loops, best of 3: 80.3 usec per loop -- Francis Avila |
| All times are GMT. The time now is 08:21 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.