472,371 Members | 1,696 Online

# Palindrome

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]
Jul 18 '05 #1
24 12805
On 13 Nov 2003 03:31:35 GMT, Runic911 wrote:
Does anyone know how i can fix my Palindrome program?

i = 0
h = 0
t = 0
p = ''
z = 0

You can start by using mnemonic variable names. These ones make the
algorithm a chore to read. Feel free to re-post it when you've done so.

--
\ "I believe in making the world safe for our children, but not |
`\ our children's children, because I don't think children should |
_o__) be having sex." -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
Knowing what your program should do, could also help.
G-:

"Runic911" <ru******@aol.com> wrote in message news:20***************************@mb-m12.aol.com...
| 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]
--
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base6 4')
Jul 18 '05 #3
Georgy Pruss wrote:
Knowing what your program should do, could also help.
G-:

As far as I know is a palindrome a word that you can read from both
sides. Sorry, but English is not my native language. So I don´t have an
English example handy. But the German name 'OTTO' is a palindrome.
--
-- Ulli
www.u-schramme.de

Jul 18 '05 #4
Hi Runic911,

You probably want this

t = p.lower()
t[h] == t[len(t)-1-h]

to do something. As it is now you don't report anything.
Looking at it some more I see it has several other problems.
The code starting at 't=p.lower()' needs to be dedented a
few levels so it is outside of the 'while s != '':' loop.
Also, that should be 'if s != '':' and likely doesn't
even need to be there at all.

In addition, you could shorten it by quite a bit by using
some of this higher-level functions available in Python.
Here's some pointers.

1) get rid of puncutation in one step rather than several.

You can use the re module for that, as in
s = "A man, a plan, a canal -- Panama!"
import re
re.sub(r'[%\$!*.,:? ;()\'\"\\-]', '', s) 'AmanaplanacanalPanama'
This is tricky, in part because the '-' must be at the end of pattern.
You could also use the translate function, as in
punctuation = '%\$!*.,-:? ;()\'\"\\'
identity = "".join([chr(i) for i in range(256)])
s.translate(identity, punctuation) 'AmanaplanacanalPanama'
This is also tricky because you have to use an identity
translation table. (On the other hand, done correctly this
could also convert everything to lower case.) I would
like a function which only removed a selected set of
characters.

Or you could make your own function for it
def remove_punctuation(s): .... letters = []
.... for c in s:
.... if c not in punctuation:
.... letters.append(c)
.... return "".join(letters)
.... remove_punctuation(s) 'AmanaplanacanalPanama'
You could put this all into your palindrome code but
it's usually easier to understand your code if each
step does one thing. By the way, an experienced
Python programmer might write this function as

def remove_punctuation(s):
return "".join([c for c in s if c not in punctuation])

(since I didn't test this one, odds are good there's
a typo ;)

2) convert everything to the same case. If you use
the regular expression way then use the lower() method,

The translate function can do it like this
for upper, lower in zip(string.ascii_uppercase, string.ascii_lowercase): .... foldcase[ord(upper)] = lower
.... foldcase = "".join(foldcase)
s 'A man, a plan, a canal -- Panama!' s.translate(foldcase, punctuation) 'amanaplanacanalpanama'
3) instead of comparing the string to itself, make a
new string which is a reverse of the old one and compare
the strings to each other

In newer Pythons (2.3) you can use [::-1] as a way
to slice the string backwards. Here's some examples
s 'A man, a plan, a canal -- Panama!' s[::-1] '!amanaP -- lanac a ,nalp a ,nam A' t = s.translate(foldcase, punctuation)
t 'amanaplanacanalpanama' t[::-1] 'amanaplanacanalpanama'
Since t == t[::-1] you know they are palindromes.

For older Pythons you'll need to turn the string
into a list of characters, reverse the list, and turn
it back into a string, as in
chars = list(s)
chars ['A', ' ', 'm', 'a', 'n', ',', ' ', 'a', ' ', 'p', 'l', 'a', 'n', ',', ' ',
'a', ' ', 'c', 'a', 'n',
'a', 'l', ' ', '-', '-', ' ', 'P', 'a', 'n', 'a', 'm', 'a', '!'] chars.reverse()
chars ['!', 'a', 'm', 'a', 'n', 'a', 'P', ' ', '-', '-', ' ', 'l', 'a', 'n', 'a',
'c', ' ', 'a', ' ', ',',
'n', 'a', 'l', 'p', ' ', 'a', ' ', ',', 'n', 'a', 'm', ' ', 'A'] "".join(chars) '!amanaP -- lanac a ,nalp a ,nam A'

This should be enough information for you to make a
very nice palindrome function.

Andrew
da***@dalkescientific.com
Jul 18 '05 #5
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]

I´m not very experienced with Python but I think you could do it in a
more simple way.
inp = raw_input('Enter a string: ')

pal = []
rev = []
for c in inp:
if c.isalnum():
pal.append(c.upper())
rev.append(c.upper())
rev.reverse()
if pal == rev:
print '"' + inp + '" ' + 'is a palindrome.'
else:
print '"' + inp + '" ' + 'is no palindrome.'

--
-- Ulli
www.u-schramme.de

Jul 18 '05 #6
Ulrich Schramme:
I´m not very experienced with Python but I think you could do it in a
more simple way.

Looks good to me. And it shows that I'm no longer able to help out
beginning programmers since my solution is along the lines of

inp = raw_input('Enter a string: ')

punctuation = '%\$!*.,-:? ;()\'\"\\'
foldcase = [chr(i) for i in range(256)]
for upper, lower in zip(string.ascii_uppercase, string.ascii_lowercase):
foldcase[ord(upper)] = lower
foldcase = "".join(foldcase)

t = inp.translate(foldcase, punctuation)
if t != t[::-1]:
print '"' + inp + '" ' + 'is a palindrome.'
else:
print '"' + inp + '" ' + 'is not a palindrome.'

Faster, but with a startup cost and a high learning curve.
It's what I would use for my own code.

A slightly easier to understand and slower version is

inp = raw_input('Enter a string: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
identity = "".join([chr(i) for i in range(256)])

t = inp.translate(identity, punctuation).lower()
if t != t[::-1]:
...

Yours is definitely easiest to understand so best for
the OP.

Cheers,
Andrew
da***@dalkescientific.com
Jul 18 '05 #7
Andrew Dalke wrote:
Ulrich Schramme:
I´m not very experienced with Python but I think you could do it in a
more simple way.

Looks good to me. And it shows that I'm no longer able to help out
beginning programmers since my solution is along the lines of

inp = raw_input('Enter a string: ')

punctuation = '%\$!*.,-:? ;()\'\"\\'
foldcase = [chr(i) for i in range(256)]
for upper, lower in zip(string.ascii_uppercase, string.ascii_lowercase):
foldcase[ord(upper)] = lower
foldcase = "".join(foldcase)

t = inp.translate(foldcase, punctuation)
if t != t[::-1]:
print '"' + inp + '" ' + 'is a palindrome.'
else:
print '"' + inp + '" ' + 'is not a palindrome.'

Faster, but with a startup cost and a high learning curve.
It's what I would use for my own code.

A slightly easier to understand and slower version is

inp = raw_input('Enter a string: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
identity = "".join([chr(i) for i in range(256)])

t = inp.translate(identity, punctuation).lower()
if t != t[::-1]:
...

Yours is definitely easiest to understand so best for
the OP.

Cheers,
Andrew
da***@dalkescientific.com

Thanks Andrew,

there might be a million ways to solve the palindrome problem. I think
that another good way would be the use of regular expressions. I´m a
software developer by profession but quite new to Python. So I tried to
find an easy way that fits my limited knowledge of the Python syntax.

My first impression of Python is that it is a well - designed and
interesting language. I also like this newsgroup. People here seem to be
more helpful than in some other groups I know...

Hopefully taking part in discussions here will improve my English as
well as my Python :-)

--
-- Ulli
www.u-schramme.de

Jul 18 '05 #8

"Ulrich Schramme" <ne**@u-schramme.de> wrote in message news:bo**********@online.de...
| Georgy Pruss wrote:
| > Knowing what your program should do, could also help.
| > G-:
| >
|
| As far as I know is a palindrome a word that you can read from both
| sides. Sorry, but English is not my native language. So I don´t have an
| English example handy. But the German name 'OTTO' is a palindrome.

I know what is a palindrome, but I can make up dozen things
the program could do with the palindrom.
G-:
|
|
| --
| -- Ulli
| www.u-schramme.de
|
Jul 18 '05 #9
Georgy Pruss wrote:
I know what is a palindrome, but I can make up dozen things
the program could do with the palindrom.
G-:

Sorry, it seems that I misunderstood your previous posting.

--
-- Ulli
www.u-schramme.de

Jul 18 '05 #10
Georgy Pruss wrote:
I know what is a palindrome, but I can make up dozen things
the program could do with the palindrom.
G-:

I´m sorry, but it seems that I misunderstood your previous posting.

--
-- Ulli
www.u-schramme.de

Jul 18 '05 #11
[Ulrich Schramme]
there might be a million ways to solve the palindrome problem. I think
that another good way would be the use of regular expressions.

The same occurred to me, so I had a go. This is as well as I was able
to do in my lunchtime.

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import re

ignore = """ ",.'`!?-"""

def isPalindrome(s):
testphrase = re.sub("[%s]" % ignore, '', s)
numtomatch = len(testphrase)/2
regx = "(\S)?"
for x in range(numtomatch):
regx = "(\S)%s(\%d)" % (regx, numtomatch-x)
rxc = re.compile(regx, re.I)
result = rxc.match(testphrase)
return result is not None

phrases = [
"Able was I, `ere I saw Elba",
"A man, a plan, a canal, Panama",
"Go Hang a Salami! I'm a Lasagna Hog!",
"Sit on a Potato Pan, Otis",
"Too Hot to Hoot",
"So Many Dynamos",
"Santa, Oscillate My Metallic Sonatas",
"Knob, testes set? Set. Bonk!",
]

if __name__ == "__main__":
print "Palindromes:"
for phrase in phrases:
if isPalindrome(phrase):
print "Yes: '%s'" % phrase
else:
print "No : '%s'" % phrase
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I'm not too happy with it though. There must be some way to have a
single fixed regular expression that can be used to test every
palindrome.

regards,

--
alan kennedy
-----------------------------------------------------
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #12
Runic911 wrote:
Does anyone know how i can fix my Palindrome program?

A palindrome program?

Are you looking for:
20:58:42:232:0 >>> def ispalindrome(s):
20:58:42:232:0 ... return s == s[::-1]
20:58:42:232:0 ...
20:58:57:232:1 >>> ispalindrome("bolton")
False
20:59:15:232:3 >>> ispalindrome("mooiezepeninepezeioom")
True
20:59:27:232:4 >>> ispalindrome("")
True

?

yours,
Gerrit.

--
276. If he hire a freight-boat, he shall pay two and one-half gerahs
per day.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #13
Alan Kennedy:
I'm not too happy with it though. There must be some way to have a
single fixed regular expression that can be used to test every
palindrome.

There isn't such a way. Regular expressions cannot match
strings of the form a**n b**n a**n (pumping lemma). That's
a palindrome so there are palindromes which cannot be
matched by regular expressions.

There are tricks. "regular expressions" aren't actually regular
and can be used to match things like a**n b**m a**n (because
of the \1 feature). However, there still isn't enough to match
a palindrome of arbitrary length. (It would be trivial to add such
a feature -- let \~1 match the reverse of the first match then
^(.*).?\~1\$
would match all palindromes... slowly. But no such feature
exists in any regexp engine I know of.)

Andrew
da***@dalkescientific.com
Jul 18 '05 #14
Gerrit Holl
Are you looking for:
20:58:42:232:0 >>> def ispalindrome(s):
20:58:42:232:0 ... return s == s[::-1]

No. The OP wanted to strip special characters and
normalize case, so that
A man, a plan, a canal -- Panama!
would be allowed as a palindrome.

Andrew
da***@dalkescientific.com
Jul 18 '05 #15
On Thu, 13 Nov 2003 23:16:50 GMT, Andrew Dalke wrote:
Gerrit Holl
Are you looking for:
20:58:42:232:0 >>> def ispalindrome(s):
20:58:42:232:0 ... return s == s[::-1]

No. The OP wanted to strip special characters and normalize case, so
that A man, a plan, a canal -- Panama! would be allowed as a
palindrome.

Here's mine.

I have yet to try this on the world's longest palindrome:

<http://www.norvig.com/palindrome.html>

=====
#!/usr/bin/env python

""" Module for palindrome determination
"""

import string

def is_palindrome( str ):
""" Determine if str is a palindrome
"""

# Assume false until proven otherwise
is_pal = False

# Remove punctuation and whitespace
passthrough_tbl = string.maketrans( '', '' )
remove_chars = string.whitespace + string.punctuation
str = string.translate( str, passthrough_tbl, remove_chars )

# Remove capitalisation
str = string.lower( str )

# Determine if massaged string matches its reverse
is_pal = ( str == str[::-1] )

return is_pal
if( __name__ == '__main__' ):
candidates = [
"",
"Foo",
"FooF",
"Foof",
"Foxof",
"Ten animals I slam in a net.",
"Lapses? Order red roses, pal.",
"A man, a plan, a canal -- Panama!",
"Satan, oscillate my metallic sonatas.",
"Eva, can I stack Rod's sad-ass, dork cats in a cave?",
]
for str in candidates:
print ( "'%s':" % str ),
print is_palindrome( str )

=====

--
\ "I wish I had a dollar for every time I spent a dollar, because |
`\ then, yahoo!, I'd have all my money back." -- Jack Handey |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #16
Ben Finney wrote:
I have yet to try this on the world's longest palindrome:

<http://www.norvig.com/palindrome.html> is_pal = ( str == str[::-1] )

For really long palindromes, you might not want to reverse the whole string:

p.endswith(p[:len(p)//2:-1])

Peter
Jul 18 '05 #17
[Alan Kennedy]
... There must be some way to have a
single fixed regular expression that can be used to test every
palindrome.

[Andrew Dalke] There isn't such a way. Regular expressions cannot match
strings of the form a**n b**n a**n (pumping lemma). That's
a palindrome so there are palindromes which cannot be
matched by regular expressions.
Thanks Andrew.

I read up on the topic after posting yesterday (because I had this
vague niggling doubt). After finding that what you stated above is
true, I realised that the vague niggling doubt was actually the memory
of my old compiler-theory lecturer laughing at me :-D

Here's a nice page I found that discusses this and other related
topics.

FINITE STATE AUTOMATA AND REGULAR EXPRESSIONS
http://www.cs.princeton.edu/introcs/71regular/
There are tricks. "regular expressions" aren't actually regular
and can be used to match things like a**n b**m a**n (because
of the \1 feature). However, there still isn't enough to match
a palindrome of arbitrary length. (It would be trivial to add such
a feature -- let \~1 match the reverse of the first match then
^(.*).?\~1\$
would match all palindromes... slowly. But no such feature
exists in any regexp engine I know of.)

The possibility of the same feature occurred to me. However, I'm still
not sure if this would solve the problem. How would the "pivot point"
be recognised by such an augmented regex engine? i.e. how would it
recognise the point at which it should stop capturing, reverse the
sequence and start matching again?

Perhaps one would need to also implement a feature whereby the length
of the entire string could be made available within expressions, so
that the size of a capture group could be limited to the first half of
the string? I.E. Something along the lines of

^(.{strlen/2}).?\~1\$

One of these days I'll find the time to dig out my old course notes
and books :#)

regards,

--
alan kennedy
-----------------------------------------------------
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #18
On Thu, 13 Nov 2003 17:32:06 +0000, Alan Kennedy <al****@hotmail.com>
wrote:

I'm not too happy with it though. There must be some way to have a
single fixed regular expression that can be used to test every
palindrome.

regards,

Thought I'd give it a try too. This is what I came up with. I used
the regular expression re.sub() function to remove the punctuation and
spaces.

The really hard part was trying to come up with a palindrome that has
the word python in it. :-)

"""
Test if a string is a palindrome.
"""
import re

def palindrome_test(p):
p = p.lower()
p = re.sub(r'\W','',p)
while p:
if p[:1] == p[-1:]:
p = p[1:-1]
else:
break
if (len(p) <= 1):
return True
else:
return False

palindrome_list = ["Bolton",
"No 'H' type, mate. No spot stops one tame
python!",
"A man, a plan, a cat, a ham, a yak, a yam, a hat,
a canal--Panama!",
"Was it a car or a cat I saw?",
"This is not a palindrome!"]

for p in palindrome_list:
print '"'+p+'"',
if palindrome_test(p):
print 'is a palindrome.'
else:
print 'is not a palindrome.'
Jul 18 '05 #19
On Fri, 14 Nov 2003 11:51:02 GMT, Ron Adam <ra****@tampabay.rr.com>
wrote:

"""
Test if a string is a palindrome.
"""
import re

def palindrome_test(p):
p = p.lower()
p = re.sub(r'\W','',p)
while p:
if p[:1] == p[-1:]:
p = p[1:-1]
else:
break
if (len(p) <= 1):
return True
else:
return False

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)

Jul 18 '05 #20
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
-----------------------------------------------------
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #21
Andrew> Gerrit Holl
Are you looking for:
20:58:42:232:0 >>> def ispalindrome(s):
20:58:42:232:0 ... return s == s[::-1]

Andrew> No. The OP wanted to strip special characters and
Andrew> normalize case, so that
Andrew> A man, a plan, a canal -- Panama!
Andrew> would be allowed as a palindrome.

import re
def ispalindrome(s):
s = re.sub("[^A-Za-z0-9]+", "", s).lower()
return s == s[::-1]

for s in (
"A man, a plan, a canal -- Panama!",
"1234",
"123321",
print s, "->", ispalindrome(s)

Skip

Jul 18 '05 #22
On Fri, 14 Nov 2003 14:34:23 +0000, Alan Kennedy <al****@hotmail.com>
wrote:

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,

Good point. Ok, how about like this?

"""
Test if a string is a palindrome.
"""
import re
def palindrome_test(p):
p = re.sub(r'\W','',p.lower())
i = len(p)//2
while i and p[i] == p[-i-1]: i -= 1
return not i

palindrome_list = [
"A",
"ab",
"Oo!",
"Bolton",
"This is not a palindrome!",
"Was it a car or a cat I saw?",
"No 'H' type, mate, no spot stops one tame python!",
"A man, a plan, a cat, a ham, a yak, a yam, a hat, a
canal--Panama!",
]

for p in palindrome_list:
print '"'+p+'"',
if palindrome_test(p):
print 'is a palindrome.'
else:
print 'is not a palindrome.'
Jul 18 '05 #23
Alan Kennedy
The possibility of the same feature occurred to me. However, I'm still
not sure if this would solve the problem. How would the "pivot point"
be recognised by such an augmented regex engine? i.e. how would it
recognise the point at which it should stop capturing, reverse the
sequence and start matching again?
Back-tracking. Suppose there was such a thing as
(.*).?\~1
where the \~1 matches the reverse of the first group.
Now match that pattern against
NOON

The (.*) matches all the way up to "NOON" then tries and
failes to match both the .? and the \~1

It backtracks one so that
(.*) matches "NOO"
and the N remains. The .? matches the N but the \~1 does not
so it backtracks so that the .? matches nothing, but still the \~1
does not match the N.

It backtracks another so that
(.*) matches "NO"
and the "ON" remains. The .? first matches with the "O" leaving
the "N", but \~1 doesn't match that, so it backtracks so that
the .? matchs nothing, leaving "ON" for the \~1. This matches!

In other words, it goes through a lot of work to do the matching,
and would take O(N) backtracks to work.

But that's not quite correct. An implementation is free to
analyze the pattern and notice that it's being asked to search
for a palindrome then insert special case code for that pattern.
Perhaps one would need to also implement a feature whereby the length
of the entire string could be made available within expressions, so
that the size of a capture group could be limited to the first half of
the string? I.E. Something along the lines of

^(.{strlen/2}).?\~1\$
Yup. That would work as well. There are all sorts of
special things one could capture in a regex-like language.
Perl 'solves' it by allowing any match to call out to embedded
Perl code, which is free to tell the matcher to stop or
continue matching.
One of these days I'll find the time to dig out my old course notes
and books :#)

Friedl's regexp book (from ORA) is quite good.

Andrew
da***@dalkescientific.com
Jul 18 '05 #24
Skip:

...

At the end are the three solutions I saw posted, from Ron Adam,
Skip, and me (modified). The times are

palindrome_skip 89.5211577111
palindrome_dalke 11.1900866758

Andrew
da***@dalkescientific.com
import re, timeit
p = re.sub(r'\W','',p.lower())
i = len(p)//2
while i and p[i] == p[-i-1]: i -= 1
return not i

# from Skip Montanaro
def palindrome_skip(s):
s = re.sub("[^A-Za-z0-9]+", "", s).lower()
return s == s[::-1]

# from Andrew Dalke
foldcase = []
punctuation = []
for i in range(256):
c = chr(i)
if c.isalnum(): # note 'alnum' instead of 'alpha' ...
foldcase.append(c.lower())
else:
foldcase.append(c)
punctuation.append(c)
foldcase = "".join(foldcase)
punctuation = "".join(punctuation)
del c, i

def palindrome_dalke(s):
t = s.translate(foldcase, punctuation)
return t == t[::-1]

verify_data = [
("A man, a plan, a canal -- Panama!", True),
("1234", False),
("123321", True),
("12321", True),
("A", True),
("ab", False),
("Oo!", True),
("Bolton", False),
("This is not a palindrome!", False),
("Was it a car or a cat I saw?", True),
("No 'H' type, mate, no spot stops one tame python!", True),
("A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal--Panama!",
True),
]

def verify():
palindrome_dalke):
for s, result in verify_data:
assert tester(s) == result, (tester, s)
print "Verified"

_time_val = None

def find_times():
global _time_val
_time_val = verify_data[-1][0] # the long "a man, a plan ... Panama"
one
"palindrome_dalke"):
timer = timeit.Timer(setup = "import __main__ as M",
stmt = "M." + tester + "(M._time_val)")
t = timer.timeit()
print tester, t

if __name__ == "__main__":
verify()
find_times()
Jul 18 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.