Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Palindrome

Reply
Thread Tools

Re: Palindrome

 
 
Pierre Quentel
Guest
Posts: n/a
 
      11-13-2003
To remove the characters that are not alphanumeric I would have used
filter :

t=filter(lambda x: x.isalnum(),list(s.lower()))

Pierre

--- In http://www.velocityreviews.com/forums/(E-Mail Removed), 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



 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      11-13-2003
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

 
Reply With Quote
 
 
 
 
Francis Avila
Guest
Posts: n/a
 
      11-13-2003
"Pierre Quentel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Palindrome using StringBuffer cat_dog_ass Java 4 01-05-2013 08:37 AM
Re: Re: Palindrome Tim Churches Python 2 06-05-2011 03:24 AM
Replacing palindrome substrings of an input string with a given string Tung Chau C Programming 1 08-06-2004 07:27 PM
Palindrome Runic911 Python 24 11-15-2003 12:08 AM
Palindrome (HELP) Lorin Leone C++ 4 11-13-2003 08:11 AM



Advertisments