palindrome

Thread Solved

Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #11
21 Days Ago
Originally Posted by kisan View Post
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #12
21 Days Ago
Here are a few things you can do with Python ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 140
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 29
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #13
21 Days Ago
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.
  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]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 140
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 29
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #14
21 Days Ago
Or you could use isalpha() like vegaseat did and stop being a chode like me.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 894
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark
 
0
  #15
21 Days Ago
You can also do this without a loop
  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. """
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC