|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ![]() |
|
#2
|
||||
|
||||
|
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 *** |
|
#3
|
|||
|
|||
|
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 ![]() |
|
#4
|
|||
|
|||
|
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 ![]() |
|
#5
|
||||
|
||||
yes stupid me: blist = alist[:] blist.reverse() is how you use it. grimey |
|
#6
|
||||
|
||||
|
I don't like the term "side effect" in this context - it implies to me that something happens in addition to wanted actions.
grimey |
|
#7
|
|||
|
|||
|
Yes Grimey,
thats the way to do it.... I too got it just now!!!!! Thanks, Subha ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > MADAM...palindrome? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|