Velocity Reviews > Palindrome

Palindrome

Runic911
Guest
Posts: n/a

 11-13-2003
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]

Ben Finney
Guest
Posts: n/a

 11-13-2003
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/>

Georgy Pruss
Guest
Posts: n/a

 11-13-2003
Knowing what your program should do, could also help.
G-:

"Runic911" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
| 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')

Ulrich Schramme
Guest
Posts: n/a

 11-13-2003
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

Andrew Dalke
Guest
Posts: n/a

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

Ulrich Schramme
Guest
Posts: n/a

 11-13-2003
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

Andrew Dalke
Guest
Posts: n/a

 11-13-2003
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
(E-Mail Removed)

Ulrich Schramme
Guest
Posts: n/a

 11-13-2003
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
> (E-Mail Removed)
>
>

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

Georgy Pruss
Guest
Posts: n/a

 11-13-2003

"Ulrich Schramme" <(E-Mail Removed)> wrote in message news:bov86k\$9hj\$(E-Mail Removed)...
| 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
|

Ulrich Schramme
Guest
Posts: n/a

 11-13-2003
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