palindrome iteration

Xavier Ho contact at xavierho.com
Fri Aug 27 05:03:35 EDT 2010


One possible reason I can think of -
"- exiting this loop means all compared chars were identical hence it
is a palindrome and i return True"

is probably incorrect reasoning. Think again.

Also, you may consider posting your code in a way that preserves the
whitespace characters.

Cheers,
Xav

On 27 August 2010 18:53, Baba <raoulbia at gmail.com> wrote:

> level: beginner
>
> the following code looks ok to me but it doesn't work. I would like
> some hints as to where my reasoning / thought goes wrong
>
> def i_palindrome(pal):
>  while len(pal)>1:
>  if pal[0] == pal[-1]:
>   pal=pal[1:-1]
>  return True
>
> print i_palindrome('annab')
>
>
> my reasoning:
> - i check the length of the string: if > 1 continue
> - i check first and last char: if they are equal continue
> - create a new, shorter string starting at index 1 and ending at
> second last index (up to but not including index-1
> -restart the while loop as long as length of string is > 1
> - exiting this loop means all compared chars were identical hence it
> is a palindrome and i return True
>
> tnx
> Baba
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100827/83e75a7d/attachment.html>


More information about the Python-list mailing list