944,017 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2591
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 4th, 2009
0
Re: palindrome
Click to Expand / Collapse  Quote originally posted by kisan ...
IT was quite helpful. so in case of palindromes how can we remove the symbols and capital letters using .upper and replace function?? for example
"Madam, in Eden I'm Adam!"
Use replace to swap out the punctuation for an empty string (''). Then use upper on both the original and the reversed string when you compare them so that the cases are the same.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 4th, 2009
0
Re: palindrome
Here are a few things you can do with Python ...
Python Syntax (Toggle Plain Text)
  1. s = "Madam, in Eden I'm Adam!"
  2.  
  3. new = ""
  4. for c in s:
  5. if c.isalpha():
  6. new += c
  7.  
  8. print(new) # MadaminEdenImAdam
  9.  
  10. # convert all characters to lower case
  11. new = new.lower()
  12.  
  13. print(new) # madaminedenimadam
  14.  
  15. # reverse new
  16. new_rev = ''.join(reversed(new))
  17.  
  18. print(new_rev) # madaminedenimadam
  19. print(new == new_rev) # True
The code can be made shorter, but than it isn't quite as readable for a beginner.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 4th, 2009
0
Re: palindrome
First of all, bravo to snippsat for a very elegant solution. I have never seen string splicing like that.

Now, how to remove the spaces and symbols? Well there is the easy inefficient way and the efficient but more difficult way.
The easy but inefficient way would be to loop through each character in the string and test if it is a space, tab, period, comma, etc. If so, then move on to the next character. If it is not one of those, then write it to a temporary string. Then test the temporary string to see if it is a palindrome and return true or false.

The problem with this method is that you're going to have a lot of if statements that you'll need to maintain. You would have to have an if statement for every symbol and non printable character on your keyboard...not to mention international symbols too. You would also run every character through a ton of tests that most likely wont apply so your code will be less efficient and more difficult to maintain. On the other hand, if this is an assignment for an entry level class this is probably all that is expected of you at this point.

The more efficient way of doing it would be to test each character against a regular expression of characters that you're interested in. If the character is in the range of a-z then copy it to a temporary string. If not, then pass on to the next character. Now you only have to maintain one if statement instead of a quadrillion. It will be more effective because it looks for a list of known acceptable characters rather than trying to list every character you're not interested in, and you will only perform one test on each character which makes your code more efficient.
python Syntax (Toggle Plain Text)
  1. mystring = 'A man, a plan, a canal, panama.'
  2.  
  3. # first, establish a temporary string and strip out the unwanted
  4. # characters
  5. tempstring = ""
  6. for char in mystring.lower():
  7. if re.match("[a-z]", char):
  8. tempstring += char
  9.  
  10. # then use the techniques described above to test for palindrome.
  11. return tempstring == tempstring[::-1]
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 4th, 2009
0
Re: palindrome
Or you could use isalpha() like vegaseat did and stop being a chode like me.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 4th, 2009
0
Re: palindrome
You can also do this without a loop
python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. import re
  3.  
  4. nonletters = re.compile("[^a-zA-Z]+")
  5.  
  6. def letters_only(mystring):
  7. return nonletters.sub(lambda m: '', mystring)
  8.  
  9. print(letters_only("Madam, in Eden I'm Adam!"))
  10.  
  11. """ my output --->
  12. MadaminEdenImAdam
  13. """
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Back with more dumb questions!
Next Thread in Python Forum Timeline: Please help me get past this problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC