Where is 'palindrome' defined?

classic Classic list List threaded Threaded
14 messages Options
fl
Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

fl
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,

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Gary Herron-2
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


Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Larry Hudson
In reply to this post by fl
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 -=-


Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

David Palao
In reply to this post by fl
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 at 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

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Omar Abou Mrad
In reply to this post by fl
On Mon, Jun 1, 2015 at 7:46 AM, fl <rxjwg98 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150601/c264c6b2/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Marko Rauhamaa
In reply to this post by fl
David Palao <dpalao.python at 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

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

David Palao
2015-06-01 11:21 GMT+02:00 Marko Rauhamaa <marko at pacujo.net>:
> David Palao <dpalao.python at 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

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Grant Edwards-7
In reply to this post by fl
On 2015-06-01, Gary Herron <gherron at 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??

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Denis McMahon
In reply to this post by fl
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 at gmail.com

fl
Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

fl
In reply to this post by fl
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

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

sohcahtoa82@gmail.com
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.

Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

MRAB-2
In reply to this post by fl
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
>


Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

John Ladasky-3
In reply to this post by Marko Rauhamaa
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.  :^)



Reply | Threaded
Open this post in threaded view
|

Where is 'palindrome' defined?

Marko Rauhamaa
John Ladasky <john_ladasky at 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