Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Checking a Number for Palindromic Behavior

Reply
Thread Tools

Re: Checking a Number for Palindromic Behavior

 
 
MRAB
Guest
Posts: n/a
 
      10-18-2009
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...
 
Reply With Quote
 
 
 
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      10-19-2009
On Oct 18, 4:20*pm, MRAB <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      10-19-2009
On Oct 19, 12:32*pm, (E-Mail Removed) wrote:
> On Oct 18, 4:20*pm, MRAB <(E-Mail Removed)> 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.
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      10-19-2009
On Oct 18, 9:45*pm, alex23 <(E-Mail Removed)> wrote:
> On Oct 19, 12:32*pm, (E-Mail Removed) wrote:
> > On Oct 18, 4:20*pm, MRAB <(E-Mail Removed)> 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.


 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      10-19-2009
(E-Mail Removed) 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...
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      10-19-2009
On Oct 18, 11:54*pm, alex23 <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
Andrew Henshaw
Guest
Posts: n/a
 
      10-19-2009
><(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>On Oct 18, 4:20 pm, MRAB <(E-Mail Removed)> 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]



 
Reply With Quote
 
Guyon Morée
Guest
Posts: n/a
 
      10-19-2009
> >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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Checking a Number for Palindromic Behavior Gary Herron Python 25 10-22-2009 05:05 PM
Checking a Number for Palindromic Behavior Benjamin Middaugh Python 0 10-18-2009 07:13 PM
Determine if a character string is palindromic lovecreatesbeauty C Programming 25 05-09-2006 09:05 PM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments