#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12

    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. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    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
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13

    yes stupid me:

    blist = alist[:]
    blist.reverse()

    is how you use it.

    grimey
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    I don't like the term "side effect" in this context - it implies to me that something happens in addition to wanted actions.

    grimey
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Yes Grimey,
    thats the way to do it....
    I too got it just now!!!!!

    Thanks,
    Subha

IMN logo majestic logo threadwatch logo seochat tools logo