Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91618 > unrolled thread

Where is 'palindrome' defined?

Started byfl <rxjwg98@gmail.com>
First post2015-05-31 21:46 -0700
Last post2015-06-02 02:10 +0100
Articles 14 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Where is 'palindrome' defined? fl <rxjwg98@gmail.com> - 2015-05-31 21:46 -0700
    Re: Where is 'palindrome' defined? Gary Herron <gherron@digipen.edu> - 2015-05-31 23:55 -0700
      Re: Where is 'palindrome' defined? Grant Edwards <invalid@invalid.invalid> - 2015-06-01 15:02 +0000
    Re: Where is 'palindrome' defined? Larry Hudson <orgnut@yahoo.com> - 2015-05-31 23:56 -0700
    Re: Where is 'palindrome' defined? David Palao <dpalao.python@gmail.com> - 2015-06-01 08:58 +0200
      Re: Where is 'palindrome' defined? Marko Rauhamaa <marko@pacujo.net> - 2015-06-01 12:21 +0300
        Re: Where is 'palindrome' defined? David Palao <dpalao.python@gmail.com> - 2015-06-01 11:41 +0200
        Re: Where is 'palindrome' defined? John Ladasky <john_ladasky@sbcglobal.net> - 2015-06-02 14:14 -0700
          Re: Where is 'palindrome' defined? Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 01:30 +0300
    Re: Where is 'palindrome' defined? Omar Abou Mrad <omar.aboumrad@gmail.com> - 2015-06-01 10:00 +0300
    Re: Where is 'palindrome' defined? Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-01 15:17 +0000
    Re: Where is 'palindrome' defined? fl <rxjwg98@gmail.com> - 2015-06-01 17:55 -0700
      Re: Where is 'palindrome' defined? sohcahtoa82@gmail.com - 2015-06-01 18:07 -0700
      Re: Where is 'palindrome' defined? MRAB <python@mrabarnett.plus.com> - 2015-06-02 02:10 +0100

#91618 — Where is 'palindrome' defined?

Fromfl <rxjwg98@gmail.com>
Date2015-05-31 21:46 -0700
SubjectWhere is 'palindrome' defined?
Message-ID<83a279a3-133d-4a50-9af3-054233bcc6af@googlegroups.com>
Hi,

When I search solution of reverse a string/number, I came across a short
function online:

>>> def palindrome(num):
	return str(num) == str(num)[::-1]

I thought that it is a general function. And with the following variable:

>>> a
'1234_5555'

>>> parlindrome(a)

Traceback (most recent call last):
  File "<pyshell#126>", line 1, in <module>
    parlindrome(a)
NameError: name 'parlindrome' is not defined


Then, I find that parlindrome is a special checking mirrored word.
I use Python 2.7.9. Why does the error message show   

name 'parlindrome' is not defined



Thanks,

[toc] | [next] | [standalone]


#91623

FromGary Herron <gherron@digipen.edu>
Date2015-05-31 23:55 -0700
Message-ID<mailman.266.1433141733.5151.python-list@python.org>
In reply to#91618
On 05/31/2015 09:46 PM, fl wrote:
> Hi,
>
> When I search solution of reverse a string/number, I came across a short
> function online:
>
>>>> def palindrome(num):
> 	return str(num) == str(num)[::-1]
>
> I thought that it is a general function. And with the following variable:

No, this  function is not built into Python because  ...  Well it's hard 
to say why.  It's not very general, or not useful to many programmers, 
or nobody's thought about it or made a case for including it in Python, etc.

But that should be no problem.  You can define it yourself (by entering 
the two line you have above).  Then it will be defined, and calling
     parlindrome('...')
will produce a result rather than an error.

Gary Herron


>
>>>> a
> '1234_5555'
>
>>>> parlindrome(a)
> Traceback (most recent call last):
>    File "<pyshell#126>", line 1, in <module>
>      parlindrome(a)
> NameError: name 'parlindrome' is not defined
>
>
> Then, I find that parlindrome is a special checking mirrored word.
> I use Python 2.7.9. Why does the error message show
>
> name 'parlindrome' is not defined
>
>
>
> Thanks,


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

[toc] | [prev] | [next] | [standalone]


#91680

FromGrant Edwards <invalid@invalid.invalid>
Date2015-06-01 15:02 +0000
Message-ID<mkhs5j$9d5$3@reader1.panix.com>
In reply to#91623
On 2015-06-01, Gary Herron <gherron@digipen.edu> wrote:
> On 05/31/2015 09:46 PM, fl wrote:
>> Hi,
>>
>> When I search solution of reverse a string/number, I came across a short
>> function online:
>>
>>>>> def palindrome(num):
>> 	return str(num) == str(num)[::-1]
>>
>> I thought that it is a general function. And with the following variable:
>
> No, this  function is not built into Python because...

Because writing a palindrome predicate is something that is only ever
done as a homework exercise in an introduction to programming class.
If it were part of the standard library, Python would not be
appropriate for such a class.

-- 
Grant Edwards               grant.b.edwards        Yow! If Robert Di Niro
                                  at               assassinates Walter Slezak,
                              gmail.com            will Jodie Foster marry
                                                   Bonzo??

[toc] | [prev] | [next] | [standalone]


#91624

FromLarry Hudson <orgnut@yahoo.com>
Date2015-05-31 23:56 -0700
Message-ID<7tOdnco9nqion_HInZ2dnUU7-dOdnZ2d@giganews.com>
In reply to#91618
On 05/31/2015 09:46 PM, fl wrote:
> Hi,
>
> When I search solution of reverse a string/number, I came across a short
> function online:
>
>>>> def palindrome(num):
> 	return str(num) == str(num)[::-1]
>
> I thought that it is a general function. And with the following variable:
>
>>>> a
> '1234_5555'
>
>>>> parlindrome(a)
>
> Traceback (most recent call last):
>    File "<pyshell#126>", line 1, in <module>
>      parlindrome(a)
> NameError: name 'parlindrome' is not defined
>
>
> Then, I find that parlindrome is a special checking mirrored word.
> I use Python 2.7.9. Why does the error message show
>
> name 'parlindrome' is not defined
>
>
>
> Thanks,
>
Don't be embarrassed, everybody does this:  You're blind to your own typos...

You define palindrome() then call parlindrome() -- with an extra 'r'.

      -=- Larry -=-

[toc] | [prev] | [next] | [standalone]


#91625

FromDavid Palao <dpalao.python@gmail.com>
Date2015-06-01 08:58 +0200
Message-ID<mailman.267.1433141892.5151.python-list@python.org>
In reply to#91618
Hi,
Because "palindrome" != "parlindrome"?
Have you read the error message? Did you try to understand it?

Best

2015-06-01 6:46 GMT+02:00 fl <rxjwg98@gmail.com>:
> Hi,
>
> When I search solution of reverse a string/number, I came across a short
> function online:
>
>>>> def palindrome(num):
>         return str(num) == str(num)[::-1]
>
> I thought that it is a general function. And with the following variable:
>
>>>> a
> '1234_5555'
>
>>>> parlindrome(a)
>
> Traceback (most recent call last):
>   File "<pyshell#126>", line 1, in <module>
>     parlindrome(a)
> NameError: name 'parlindrome' is not defined
>
>
> Then, I find that parlindrome is a special checking mirrored word.
> I use Python 2.7.9. Why does the error message show
>
> name 'parlindrome' is not defined
>
>
>
> Thanks,
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#91637

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-06-01 12:21 +0300
Message-ID<87h9qraimu.fsf@elektro.pacujo.net>
In reply to#91625
David Palao <dpalao.python@gmail.com>:

> Because "palindrome" != "parlindrome"?
> Have you read the error message? Did you try to understand it?

When you are starting with any new thing, even the simplest problems
look baffling. Once you have achieved a few successes, such error
messages start to make sense.

>>>>> def palindrome(num):
>>         return str(num) == str(num)[::-1]

BTW, this simplistic function is not very practical for serious,
real-life palindromes. It does work for the naïve Finnish palindrome:

    saippuakauppias
    (= "a soap merchant")

However, it fails to detect:

    innostunut sonni
    (= "an enthusiastic bull")

let alone:

    Sota-apina nakataan raastimeen.
          Apelle pane emit.
      Saarnaa takanani paatos.

    (= "The war monkey will be chucked into a grater.
        Hand the pistils to father-in-law.
        Pathos does preach behind me.")


Marko

[toc] | [prev] | [next] | [standalone]


#91641

FromDavid Palao <dpalao.python@gmail.com>
Date2015-06-01 11:41 +0200
Message-ID<mailman.275.1433151710.5151.python-list@python.org>
In reply to#91637
2015-06-01 11:21 GMT+02:00 Marko Rauhamaa <marko@pacujo.net>:
> David Palao <dpalao.python@gmail.com>:
>
>> Because "palindrome" != "parlindrome"?
>> Have you read the error message? Did you try to understand it?
>
> When you are starting with any new thing, even the simplest problems
> look baffling. Once you have achieved a few successes, such error
> messages start to make sense.
>

I didn't intend to be harsh. Sorry if I failed.
Anyway, for me the errors provided by python are mostly very useful.
When I forget to read them, or I'm simply too impatient to try to
understand them, I use to get very hard debugging sessions. The
contrary is often true.
What I tried to emphasize is the importance of paying careful
attention to the error messages. It is a good habit to start doing
this from the beginning.

Best

[toc] | [prev] | [next] | [standalone]


#91896

FromJohn Ladasky <john_ladasky@sbcglobal.net>
Date2015-06-02 14:14 -0700
Message-ID<0600731d-d2b1-4045-967e-0c7e4fdcb4c7@googlegroups.com>
In reply to#91637
On Monday, June 1, 2015 at 2:22:02 AM UTC-7, Marko Rauhamaa wrote:

>     Sota-apina nakataan raastimeen.
>           Apelle pane emit.
>       Saarnaa takanani paatos.
> 
>     (= "The war monkey will be chucked into a grater.
>         Hand the pistils to father-in-law.
>         Pathos does preach behind me.")

With this post, you have convinced me that 1) Finnish is a very interesting, and even poetic, language; and that 2) eating the velvet off of reindeer antlers must have a very similar effect to LSD.  :^)

[toc] | [prev] | [next] | [standalone]


#91908

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-06-03 01:30 +0300
Message-ID<8761757ngh.fsf@elektro.pacujo.net>
In reply to#91896
John Ladasky <john_ladasky@sbcglobal.net>:

> On Monday, June 1, 2015 at 2:22:02 AM UTC-7, Marko Rauhamaa wrote:
>
>>     Sota-apina nakataan raastimeen.
>>           Apelle pane emit.
>>       Saarnaa takanani paatos.
>> 
>>     (= "The war monkey will be chucked into a grater.
>>         Hand the pistils to father-in-law.
>>         Pathos does preach behind me.")
>
> With this post, you have convinced me that 1) Finnish is a very
> interesting, and even poetic, language; and that 2) eating the velvet
> off of reindeer antlers must have a very similar effect to LSD. :^)

The phonetic structure of Finnish is ideal for palindromes, unlike
English.


Marko

[toc] | [prev] | [next] | [standalone]


#91626

FromOmar Abou Mrad <omar.aboumrad@gmail.com>
Date2015-06-01 10:00 +0300
Message-ID<mailman.268.1433142041.5151.python-list@python.org>
In reply to#91618

[Multipart message — attachments visible in raw view] — view raw

On Mon, Jun 1, 2015 at 7:46 AM, fl <rxjwg98@gmail.com> wrote:

> Hi,
>
> When I search solution of reverse a string/number, I came across a short
> function online:
>
> >>> def palindrome(num):
>         return str(num) == str(num)[::-1]
>
> I thought that it is a general function. And with the following variable:
>
> >>> a
> '1234_5555'
>
> >>> parlindrome(a)
>
> Traceback (most recent call last):
>   File "<pyshell#126>", line 1, in <module>
>     parlindrome(a)
> NameError: name 'parlindrome' is not defined
>
> <snip>


Further to the mentioned, be careful with your spelling, the function name
as you've shown is "palindrome" but you're invoking it using "parlindrome".

Regards

[toc] | [prev] | [next] | [standalone]


#91684

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-06-01 15:17 +0000
Message-ID<mkht2s$hvk$2@dont-email.me>
In reply to#91618
On Sun, 31 May 2015 21:46:31 -0700, fl wrote:

>>>> def palindrome(num):

Note carefully the spelling(1): palindrome

>>>> parlindrome(a)

Note carefully the spelling(2): parlindrome

> NameError: name 'parlindrome' is not defined

Compare carefully spelling(1) and spelling(2).

palindrome is defined, pa_r_lindrome is not defined. The computer is not 
psychic yet, so it doesn't know that when you wrote pa_r_lindrome you 
meant palindrome, you have to spell it the same way every time for the 
computer to recognise that you mean the same name.

Now you also know that the error message:

"NameError: name '<something>' is not defined"

means that you might have spelled <something> differently on the line in 
the error message to the word you meant, so next time you see this error 
message you know to carefully check the spellings of function names and 
variables.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#91739

Fromfl <rxjwg98@gmail.com>
Date2015-06-01 17:55 -0700
Message-ID<888b51eb-0ede-4120-a914-aeec45d6366d@googlegroups.com>
In reply to#91618
On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote:
> Hi,
> 
> When I search solution of reverse a string/number, I came across a short
> function online:
> 
> >>> def palindrome(num):
> 	return str(num) == str(num)[::-1]
> 
> I thought that it is a general function. And with the following variable:
> 
> >>> a
> '1234_5555'
> 
> >>> parlindrome(a)
> 
> Traceback (most recent call last):
>   File "<pyshell#126>", line 1, in <module>
>     parlindrome(a)
> NameError: name 'parlindrome' is not defined
> 
> 
> Then, I find that parlindrome is a special checking mirrored word.
> I use Python 2.7.9. Why does the error message show   
> 
> name 'parlindrome' is not defined
> 
> 
> 
> Thanks,

Thanks, I realize that it was spelled wrong. Now, with the correct spelling
the result is a 'False'. I have expected it gives reversed string. Is the 
function behaves correct or not?


Thanks,




>>> a='1234'
>>> def palindrome(num):
	return str(num) == str(num)[::-1]
>>> palindrome(a)
False
>>> palindrome("fread")
False

[toc] | [prev] | [next] | [standalone]


#91748

Fromsohcahtoa82@gmail.com
Date2015-06-01 18:07 -0700
Message-ID<2698673b-3867-41dd-bd02-18a6c768cadf@googlegroups.com>
In reply to#91739
On Monday, June 1, 2015 at 5:55:14 PM UTC-7, fl wrote:
> On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote:
> > Hi,
> > 
> > When I search solution of reverse a string/number, I came across a short
> > function online:
> > 
> > >>> def palindrome(num):
> > 	return str(num) == str(num)[::-1]
> > 
> > I thought that it is a general function. And with the following variable:
> > 
> > >>> a
> > '1234_5555'
> > 
> > >>> parlindrome(a)
> > 
> > Traceback (most recent call last):
> >   File "<pyshell#126>", line 1, in <module>
> >     parlindrome(a)
> > NameError: name 'parlindrome' is not defined
> > 
> > 
> > Then, I find that parlindrome is a special checking mirrored word.
> > I use Python 2.7.9. Why does the error message show   
> > 
> > name 'parlindrome' is not defined
> > 
> > 
> > 
> > Thanks,
> 
> Thanks, I realize that it was spelled wrong. Now, with the correct spelling
> the result is a 'False'. I have expected it gives reversed string. Is the 
> function behaves correct or not?
> 
> 
> Thanks,
> 
> 
> 
> 
> >>> a='1234'
> >>> def palindrome(num):
> 	return str(num) == str(num)[::-1]
> >>> palindrome(a)
> False
> >>> palindrome("fread")
> False

If you want the reversed string, then just use a[::-1].  Your palindrome function simply returns a boolean telling you whether or not the string you gave it is a palindrome.

[toc] | [prev] | [next] | [standalone]


#91751

FromMRAB <python@mrabarnett.plus.com>
Date2015-06-02 02:10 +0100
Message-ID<mailman.30.1433207459.13271.python-list@python.org>
In reply to#91739
On 2015-06-02 01:55, fl wrote:
> On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote:
>> Hi,
>>
>> When I search solution of reverse a string/number, I came across a short
>> function online:
>>
>> >>> def palindrome(num):
>> 	return str(num) == str(num)[::-1]
>>
>> I thought that it is a general function. And with the following variable:
>>
>> >>> a
>> '1234_5555'
>>
>> >>> parlindrome(a)
>>
>> Traceback (most recent call last):
>>   File "<pyshell#126>", line 1, in <module>
>>     parlindrome(a)
>> NameError: name 'parlindrome' is not defined
>>
>>
>> Then, I find that parlindrome is a special checking mirrored word.
>> I use Python 2.7.9. Why does the error message show
>>
>> name 'parlindrome' is not defined
>>
>>
>>
>> Thanks,
>
> Thanks, I realize that it was spelled wrong. Now, with the correct spelling
> the result is a 'False'. I have expected it gives reversed string. Is the
> function behaves correct or not?
>
>
It compares 'num' as a string to the reverse of that to test whether 
it's a palindrome,
so, yes, it's behaving correctly.

Perhaps its purpose would have been clearer if it had been called
"is_palindrome".

>
>>>> a='1234'
>>>> def palindrome(num):
> 	return str(num) == str(num)[::-1]
>>>> palindrome(a)
> False
>>>> palindrome("fread")
> False
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web