Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2004, 05:12 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
MADAM...palindrome?

Code:
>>> def palindrome(myString):
... 	original=list(myString)
... 	reverse=[]
... 	temp=original
... 	for element in range(len(myString)):
... 		reverse.append(temp.pop())
... 	print temp,original,reverse
... 	if original==reverse:
... 		print myString,'is a palindrome'
... 	else: print myString,'is not a palindrome'
... 
>>> palindrome('india')
[] [] ['a', 'i', 'd', 'n', 'i']
india is not a palindrome
>>> palindrome('madam')
[] [] ['m', 'a', 'd', 'a', 'm']
madam is not a palindrome


The problem is clearly visible but I don't know why its happening. I mean temp and original are two different variables but they are taken as one. temp is taken as a reference of original which I don't want it to happen. How can I prevent this? This is the way I've done in my C/C++...doesn't work out here!!!

Thanks & Rgds,
Subha

Reply With Quote
  #2  
Old October 29th, 2004, 05:50 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
For anything more complicated than a number Python uses references (pointer like things). So you have to manually create copies of objects like strings and lists if you want to keep the original untouched.

Python has very powerful tools that help:

To make a copy of the original string you take a full slice:
acopy = original[:]

However using a neat trick with the extended slice you can also reverse the order like this:
reversecopy = original[::-1]

If you wish to use lists then you can make use of a built in reverse method:

originalList = list(mystring)
reverselist = originalList.reverse()

or again a reverse slice works too:

reverselist = originalList[::-1]

I recommend a read of the Python 2.3 Quick Reference as this has all the datatypes and their methods all in one place.
grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old October 29th, 2004, 06:12 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Thanks Grim!!!
Awesome....actually I forgot abt taking the full slice

But
reversecopy = original[::-1] is a smater way of doing it!!

Hey I tried using the reverse method but it didn't give me anything. Then sought the help of the documentation only to find this....

The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list.


So let me know how to use it???

Thanks & Rgds,
Subha

Reply With Quote
  #4  
Old October 29th, 2004, 06:17 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Oops!!! theres a mistake here....
The reverse method works like its intended to...
but I don't know why it wasn't giving the expected output all the times I tried...but now suddenly its working...

God knows...theres something fishy!!!!

Thanks,
Subha

Reply With Quote
  #5  
Old October 29th, 2004, 06:17 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon

yes stupid me:

blist = alist[:]
blist.reverse()

is how you use it.

grimey

Reply With Quote
  #6  
Old October 29th, 2004, 06:21 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
I don't like the term "side effect" in this context - it implies to me that something happens in addition to wanted actions.

grimey

Reply With Quote
  #7  
Old October 29th, 2004, 06:22 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Yes Grimey,
thats the way to do it....
I too got it just now!!!!!

Thanks,
Subha

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > MADAM...palindrome?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT