Re: Checking a Number for Palindromic Behavior

Discussion in 'Python' started by MRAB, Oct 18, 2009.

  1. MRAB

    MRAB Guest

    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... :)
     
    MRAB, Oct 18, 2009
    #1
    1. Advertising

  2. MRAB

    Guest

    On Oct 18, 4:20 pm, MRAB <> 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.
     
    , Oct 19, 2009
    #2
    1. Advertising

  3. MRAB

    alex23 Guest

    On Oct 19, 12:32 pm, wrote:
    > On Oct 18, 4:20 pm, MRAB <> 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.
     
    alex23, Oct 19, 2009
    #3
  4. MRAB

    Guest

    On Oct 18, 9:45 pm, alex23 <> wrote:
    > On Oct 19, 12:32 pm, wrote:
    > > On Oct 18, 4:20 pm, MRAB <> 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.


    I hardly think that code is evidence of cleverness.

    Re helping the OP learn...

    MRAB (and gherron) were wrong.

    One, it was suggested without any evidence the the OP was
    "probably" asking about homework. My observation over
    several years is that this group has a very poor record
    of identifying homework problems. And if someone can
    conclude that the OPs problem was homework with no evidence,
    then I can conclude that it wasn't, without evidence,
    equally validly.

    As for learning better by "working it out oneself", that is
    a myth. I know it is not (universally) true by counter-example:
    I once asked a question here got several answers. One
    person explained the behavior I'd not understood in a single
    sentence. Another provided a long transcript of an interactive
    Python session intended to demonstrate the behavior and its
    origin. I read the first response and it was immediately
    clear what my misunderstanding was, and why Python was doing
    what I hadn't understood. I never bothered with the long
    demo since it would have been a waste of my time, despite
    that the poster had obviously put significant time into
    writing it.

    I realize that in many courses the pedagogy involves working
    out the basic ideas, theorems, etc. But the environment
    of a course taught by an instructor thoughorly covering an
    organized range of topics is totally different than a usenet
    newsgroup.

    Different people have different goals and learning styles.
    I gave the OP another option. He can choose whichever
    helps him learn most effectively. That is a *good* thing.
     
    , Oct 19, 2009
    #4
  5. MRAB

    alex23 Guest

    wrote:
    > One, it was suggested without any evidence the the OP was
    > "probably" asking about homework.  My observation over
    > several years is that this group has a very poor record
    > of identifying homework problems.  And if someone can
    > conclude that the OPs problem was homework with no evidence,
    > then I can conclude that it wasn't, without evidence,
    > equally validly.


    I've always seen the accusation of "homework problem" to be less about
    homework and more about an attempt to get other people to provide
    working code. In this case, the absence of evidence (ie posting what
    code has been tried) _is_ indicative of such problems.

    This isn't about being churlish, it's about not being able to identify
    the experience level at which such posters are operating, about not
    wasting time providing answers that have already been tried & rejected
    etc etc

    > As for learning better by "working it out oneself", that is
    > a myth.


    MRAB broke the OP's request down into already-understood-by-the-OP
    operations; being able to extrapolate from that is _essential_ to
    becoming a programmer. I'm not sure how you think that giving someone
    an answer will ever help them be able to answer similar questions
    themselves. The process followed to achieve the solution is what is
    important here, not the solution itself.

    Give a programmer a code fragment, and you've helped him for a day.
    Teach a programmer to code, however...
     
    alex23, Oct 19, 2009
    #5
  6. MRAB

    Guest

    On Oct 18, 11:54 pm, alex23 <> wrote:
    > wrote:
    >> One, it was suggested without any evidence the the OP was
    >> "probably" asking about homework. My observation over
    >> several years is that this group has a very poor record
    >> of identifying homework problems. And if someone can
    >> conclude that the OPs problem was homework with no evidence,
    >> then I can conclude that it wasn't, without evidence,
    >> equally validly.

    >
    > I've always seen the accusation of "homework problem" to be less about
    > homework and more about an attempt to get other people to provide
    > working code. In this case, the absence of evidence (ie posting what
    > code has been tried) _is_ indicative of such problems.


    I don't share your view. Whether or not a question is a
    homework problem, and whether or not the questioner has
    provided some indication he/she has tried to solve it
    before posting are orthogonal issues, and I have not
    noticed any strong correlation between them in posts to
    this group.

    If you feel a poster is just trying to get you to do
    their work for them while putting in no effort themselves,
    you are perfectly free not to respond, or to ask them to
    demonstrate some effort. I said nothing opposing this.

    > This isn't about being churlish, it's about not being able to identify
    > the experience level at which such posters are operating, about not
    > wasting time providing answers that have already been tried & rejected
    > etc etc


    Again, feel free to ask the OP for whatever information
    *you* feel *you* need to respond effectively.

    >> As for learning better by "working it out oneself", that is
    >> a myth.

    >
    > MRAB broke the OP's request down into already-understood-by-the-OP
    > operations; being able to extrapolate from that is _essential_ to
    > becoming a programmer. I'm not sure how you think that giving someone
    > an answer will ever help them be able to answer similar questions
    > themselves. The process followed to achieve the solution is what is
    > important here, not the solution itself.


    Right. But you are concluding that only by figuring out
    the process by reconstructing it from hints can it be
    learned. I disagree with that and maintain that you
    can show someone a solution, and they can figure out
    quite well, seeing the answer worked out, how to get
    similar answers in the future. In more detail...

    [Disclaimer: I know nothing about educational methods
    beyond what I have experienced -- these are my personal
    opinions.]

    There are (at least) two ways to learn something. You
    can adopt an experiential approach by trying to repeat
    the "discovery" of the knowledge (guided by a teacher
    to avoid wasting time, get past too-hard parts, etc).

    Another way (probably more common) is to simply have
    someone tell you the facts and you make the effort to
    remember them and integrate them with what you already
    know.

    The first seems to work well when one is facing a large
    body of knowledge that one is learning about for the
    first time, more or less independent of anything else
    in one's life (e.g. the typical collage course).

    The second seems to working well when you already
    have a surrounding knowledge structure to integrate
    the new information with, for example a specific
    problem you are trying to solve.

    I would also imagine that individual people have a
    bias toward one or the other approaches as well.

    It seems to me that the conceptual barrier facing the
    OP was thinking he had to look at each digit of his
    number separately. The two key bits to getting past
    that is, "consider the number as a string", and (what
    MRAB said), "It's a palindrome if it's [string is] the]
    same as its reverse."

    Everything past that are python-specific implementation
    details, specifically conversion of numbers to strings,
    the existence of the reversed function, and how to
    deal with the funky value returned by reversed.

    My guess was the conceptual barrier was the big one,
    and although the OP was learning Python, was likely
    to be able to learn about the parts of Python needed
    to implement a solution simply by looking at a solution
    (and reading the docs regarding the bits and pieces,
    str(), reveresed(), join(), etc) -- no need to jump
    though a bunch of hoops set up for him by (well-
    meaning) people copying a pedagogy developed for use
    in a completely different environment.

    Again, I am *not* saying my evaluation of the OP's
    needs is correct, only taking issue with your (and
    several other's) seeming insistence that *only* the
    experiential response was appropriate.

    Repeating what I said in my previous post:
    Different people have different goals and learning
    styles. Choice is *good*.

    > Give a programmer a code fragment, and you've helped him for a day.
    > Teach a programmer to code, however...


    Yes, but be sure you're dealing with a programmer first,
    and not someone who just wants to use a program to help
    solve a problem.
     
    , Oct 19, 2009
    #6
  7. ><> wrote in message
    >news:...
    >On Oct 18, 4:20 pm, MRAB <> 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.


    I wouldn't normally provide a direct solution to this type of request; but
    since you have, may I suggest:

    def is_palindrome (n):
    return str(n) == str(n)[::-1]
     
    Andrew Henshaw, Oct 19, 2009
    #7
  8. MRAB

    Guyon Morée Guest

    > >def is_palidrome (n):
    > >    return str(n) == ''.join (reversed (str(n)))

    >
    > >which will return True if integer n is a palidromic or False
    > >otherwise.

    >
    > I wouldn't normally provide a direct solution to this type of request; but
    > since you have, may I suggest:
    >
    > def is_palindrome (n):
    >     return str(n) == str(n)[::-1]



    http://gumuz.nl/weblog/python-trick-sequence-reversal/
     
    Guyon Morée, Oct 20, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    428
  2. Mantorok Redgormor
    Replies:
    70
    Views:
    1,764
    Dan Pop
    Feb 17, 2004
  3. lovecreatesbeauty

    Determine if a character string is palindromic

    lovecreatesbeauty, May 8, 2006, in forum: C Programming
    Replies:
    25
    Views:
    910
    Denis Kasak
    May 9, 2006
  4. Benjamin Middaugh

    Checking a Number for Palindromic Behavior

    Benjamin Middaugh, Oct 18, 2009, in forum: Python
    Replies:
    0
    Views:
    243
    Benjamin Middaugh
    Oct 18, 2009
  5. Gary Herron
    Replies:
    25
    Views:
    686
Loading...

Share This Page