Checking a Number for Palindromic Behavior

alex23 wuwei23 at gmail.com
Sun Oct 18 23:45:47 EDT 2009


On Oct 19, 12:32 pm, ru... at yahoo.com wrote:
> On Oct 18, 4:20 pm, MRAB <pyt... at mrabarnett.plus.com> wrote:
>
> > Benjamin Middaugh wrote:
> > > Thanks to everyone who helped with my query on reversing integers. I
> > > have one more simple problem I'm having trouble solving. I want to check
> > > a number for palindromic behavior (reading the same backwards and
> > > forwards). So if I have an integer 1457 it can tell me this is not the
> > > same from both ends but 1551 is. I think the simplest way would be to
> > > work inwards from both ends checking digits for equality, but I don't
> > > know enough (yet) to do this.
>
> > > All help is much appreciated.
>
> > It's a palindrome if it's the same as its reverse. You already know how
> > to reverse it, and you should already know how to check whether two
> > things are the same, so... :-)
>
> Something like:
>
> def is_palidrome (n):
>     return str(n) == ''.join (reversed (str(n)))
>
> which will return True if integer n is a palidromic or False
> otherwise.

_Not_ providing the code was MRAB's entire point here. It's nice to
show that you're clever and all, but that doesn't really help the OP
learn.



More information about the Python-list mailing list