| | |
palindrome
Thread Solved |
0
#11 21 Days Ago
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.
0
#12 21 Days Ago
Here are a few things you can do with Python ...
The code can be made shorter, but than it isn't quite as readable for a beginner.
Python Syntax (Toggle Plain Text)
s = "Madam, in Eden I'm Adam!" new = "" for c in s: if c.isalpha(): new += c print(new) # MadaminEdenImAdam # convert all characters to lower case new = new.lower() print(new) # madaminedenimadam # reverse new new_rev = ''.join(reversed(new)) print(new_rev) # madaminedenimadam print(new == new_rev) # True
May 'the Google' be with you!
•
•
Join Date: Nov 2007
Posts: 140
Reputation:
Solved Threads: 29
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.
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)
mystring = 'A man, a plan, a canal, panama.' # first, establish a temporary string and strip out the unwanted # characters tempstring = "" for char in mystring.lower(): if re.match("[a-z]", char): tempstring += char # then use the techniques described above to test for palindrome. return tempstring == tempstring[::-1]
0
#15 21 Days Ago
You can also do this without a loop
python Syntax (Toggle Plain Text)
#!/usr/bin/env python import re nonletters = re.compile("[^a-zA-Z]+") def letters_only(mystring): return nonletters.sub(lambda m: '', mystring) print(letters_only("Madam, in Eden I'm Adam!")) """ my output ---> MadaminEdenImAdam """
![]() |
Similar Threads
- To form a palindrome of a given string (C)
- Help on stack , queue, palindrome program... (C++)
- palindrome program (Legacy and Other Languages)
- stack palindrome problem? (C++)
Other Threads in the Python Forum
- Previous Thread: Palindrome Checking (Python)
- Next Thread: Please help me get past this problem
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input itunes java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite ssh statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






