473,842 Members | 1,337 Online

# Palindrome

Does anyone know how i can fix my Palindrome program?

s = raw_input('Ente r a String: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
i = 0
h = 0
t = 0
p = ''
z = 0
while s!= ' ':
while i <= len(s) - 1:
punct = s[i]
if punctuation.fin d(punct) == -1:
p = p + punct
i = i + 1
t = p.lower()
t[h] == t[len(t)-1-h]
Jul 18 '05 #1
24 13031
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.c om> wrote in message news:20******** *************** ****@mb-m12.aol.com...
| Does anyone know how i can fix my Palindrome program?
|
| s = raw_input('Ente r a String: ')
| punctuation = '%\$!*.,-:? ;()\'\"\\'
| i = 0
| h = 0
| t = 0
| p = ''
| z = 0
| while s!= ' ':
| while i <= len(s) - 1:
| punct = s[i]
| if punctuation.fin d(punct) == -1:
| p = p + punct
| i = i + 1
| t = p.lower()
| t[h] == t[len(t)-1-h]
--
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')
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) 'Amanaplanacana lPanama'
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(ide ntity, punctuation) 'Amanaplanacana lPanama'
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_punctuat ion(s): .... letters = []
.... for c in s:
.... if c not in punctuation:
.... letters.append( c)
.... return "".join(letters )
.... remove_punctuat ion(s) 'Amanaplanacana lPanama'
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_punctuat ion(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.asci i_uppercase, string.ascii_lo wercase): .... foldcase[ord(upper)] = lower
.... foldcase = "".join(foldcas e)
s 'A man, a plan, a canal -- Panama!' s.translate(fol dcase, punctuation) 'amanaplanacana lpanama'
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(fol dcase, punctuation)
t 'amanaplanacana lpanama' t[::-1] 'amanaplanacana lpanama'
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(cha rs) '!amanaP -- lanac a ,nalp a ,nam A'

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

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #5
Runic911 wrote:
Does anyone know how i can fix my Palindrome program?

s = raw_input('Ente r a String: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
i = 0
h = 0
t = 0
p = ''
z = 0
while s!= ' ':
while i <= len(s) - 1:
punct = s[i]
if punctuation.fin d(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('Ente r a string: ')

pal = []
rev = []
for c in inp:
if c.isalnum():
pal.append(c.up per())
rev.append(c.up per())
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('Ente r a string: ')

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

t = inp.translate(f oldcase, 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('Ente r a string: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
identity = "".join([chr(i) for i in range(256)])

t = inp.translate(i dentity, punctuation).lo wer()
if t != t[::-1]:
...

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

Cheers,
Andrew
da***@dalkescie ntific.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('Ente r a string: ')

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

t = inp.translate(f oldcase, 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('Ente r a string: ')
punctuation = '%\$!*.,-:? ;()\'\"\\'
identity = "".join([chr(i) for i in range(256)])

t = inp.translate(i dentity, punctuation).lo wer()
if t != t[::-1]:
...

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

Cheers,
Andrew
da***@dalkescie ntific.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

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