Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Palindrome (http://www.velocityreviews.com/forums/t324880-re-palindrome.html)

Pierre Quentel 11-13-2003 04:01 PM

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




Alex Martelli 11-13-2003 06:03 PM

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


Francis Avila 11-13-2003 06:11 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57