473,378 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

palindrome function

kdt
Hi all,

Can someone please explain to me why the following evaluates as false?
>>>list=['a','n','n','a']
list==list.reverse()
False
I'm stumped :s
Jul 11 '08 #1
10 1645
On Sat, Jul 12, 2008 at 12:22 AM, kdt <do********@googlemail.comwrote:
Hi all,

Can someone please explain to me why the following evaluates as false?
>>>>list=['a','n','n','a']
list==list.reverse()
False

I'm stumped :s
Read the documentation on list.reverse().

Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"

Also, 'list' is a really bad name for a list, since this is the name of
the builtin type object for the list type.

--
Denis Kasak
Jul 11 '08 #2
kdt
On Jul 11, 11:34*pm, Denis Kasak <denis.kasak2718281...@gmail.com>
wrote:
On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjeta...@googlemail.comwrote:

*Hi all,
*>
*Can someone please explain to me why the following evaluates as false?
*>
*>>>>list=['a','n','n','a']
*>>>>list==list.reverse()
*>>>>False
*>
*I'm stumped :s

Read the documentation on list.reverse().

Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
* * print "Palindrome"

Also, 'list' is a really bad name for a list, since this is the name of
the builtin type object for the list type.

--
Denis Kasak
thanks for the explanation :D
Jul 11 '08 #3
Denis Kasak:
spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"
An alternative version:
>>txt = "anna"
txt == txt[::-1]
True
>>txt = "annabella"
txt == txt[::-1]
False

Bye,
bearophile
Jul 11 '08 #4
On Jul 11, 5:34*pm, Denis Kasak <denis.kasak2718281...@gmail.com>
wrote:
On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjeta...@googlemail.comwrote:

*Hi all,
*>
*Can someone please explain to me why the following evaluates as false?
*>
*>>>>list=['a','n','n','a']
*>>>>list==list.reverse()
*>>>>False
*>
*I'm stumped :s

Read the documentation on list.reverse().

Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
* * print "Palindrome"
You could also do
>>spam = ['a','n','n','a']
if spam == [i for i in reversed(spam)]:
print "Palindrome"

>
Also, 'list' is a really bad name for a list, since this is the name of
the builtin type object for the list type.

--
Denis Kasak
Jul 11 '08 #5
On Jul 11, 6:20*pm, Mensanator <mensana...@aol.comwrote:
Try this:
spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
* * print "Palindrome"

You could also do
>spam = ['a','n','n','a']
if spam == [i for i in reversed(spam)]:

* * * * print "Palindrome"
Or instead of this:

[ i for i in generator_expression]

just use the list constructor:

list(generator_expression)

-- Paul
Jul 12 '08 #6
Denis Kasak wrote:
Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"
Your explanation is correct, but your example code compares None to
['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.

Peter

Jul 12 '08 #7
Peter Otten wrote:
Denis Kasak wrote:
>Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"

Your explanation is correct, but your example code compares None to
['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.
Of course. Thank you for the correction. I guess you know your caffeine
has started to wear off when you start making the same mistakes you were
trying to fix. :-)

--
Denis Kasak
Jul 12 '08 #8


Peter Otten wrote:
Denis Kasak wrote:
>Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"

Your explanation is correct, but your example code compares None to
['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.
I don't know if this was posted yet, but 'seq.reversed() == seq' is the
simple way to test for 'palindomeness'.

Jul 12 '08 #9
On Jul 12, 2:18�pm, Terry Reedy <tjre...@udel.eduwrote:
Peter Otten wrote:
Denis Kasak wrote:
Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn't return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:
spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
if spam.reverse() == eggs:
� � print "Palindrome"
Your explanation is correct, but your example code compares None to
['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.

I don't know if this was posted yet,
It hasn't. and here's why:

IDLE 2.6b1
>>seq=['a','n','n','a']
seq.reversed()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
seq.reversed()
AttributeError: 'list' object has no attribute 'reversed'

but 'seq.reversed() == seq' is the
simple way to test for 'palindomeness'.
Not in 2.x.

Jul 12 '08 #10


Mensanator wrote:
It hasn't. and here's why:

IDLE 2.6b1
>>>seq=['a','n','n','a']
seq.reversed()

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
seq.reversed()
AttributeError: 'list' object has no attribute 'reversed'
My apologies. reversed() is a builtin func, not a method, and it
produces an iterator, not a seq. SO, corrected,
>>for s in ((1,2,3,2,1), [1,2,3,2,1]):
.... type(s)(reversed(s)) == s
....
True
True
>>s = '12121'
''.join(reversed(s)) == s
True

Jul 13 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
23
by: Amar Prakash Tripaithi | last post by:
Dan Hoey, who had recently graduated, wrote a C program to look for and construct the following beauty: A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa,...
32
by: ramakrishnadeepak | last post by:
HI Everybody, I 've to submit a program on c.Can any one help me plz.........The problem is like this:: Write a program which computes the largest palindrome substring of a string. Input:...
4
by: outofmymind | last post by:
hi, im trying to solve the following question: Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob"); ...
3
by: colinNeedsJavaHelp | last post by:
I am still having an exceptional amount of trouble with java. This is my new assignment, if anyone can help I would greatly appreciate it. I don't even know where to start. A word or phrase in...
2
by: Synapse | last post by:
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it. If I enter 33633, 11211, 45554, it will...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
3
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no...
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.