Byte of Python The 111th page about palindrome work exercises, originally titled: to check whether the text is a palindrome need to ignore the punctuation, space and case. For example,"Rise to vote, sir." is a palindrome text, but our existing program does not think so. Can you improve the program above so that it can recognize this palindrome? If you need some hints, here's an idea: use a tuple to save all the characters that need to be disabled, and then use the membership test to determine if a character should be removed, i.e. Forbidden = ( ! ,? , . , ...) . -- original book note
The key to the problem is how to find out the special characters in the string, you can use the method in the hint, it is necessary to note that after the deletion of an element, the original array will change, so pay attention to the problem of ordinal in the traversal process.
My code is as follows:
" "This was the readme of this project! Is you OK?" "Forbindden_word= (' ',',','.','!','//','?')defIgnore_word (text):#remove special characters that are ignoredStrdemo =list (text) Count=0 forIinchlist (range (len (Strdemo ))):ifStrdemo[i-count]inchForbindden_word:delStrdemo[i-count]#Remove special charactersCount + = 1result="'. Join (Strdemo)returnresultdefreverse (text):returnText[::-1]defIs_reverse (text):#Judging whether it is a palindrome returnText = =reverse (text) something= Input ('Please input some text:')ifIs_reverse (Ignore_word (something)):Print(Ignore_word (something))Print('yes! This is a reverse sentense.')Else : Print(Ignore_word (something))Print('oh! No! This is a reverse sentense!')
Byte of Python Learning Note (2)--palindrome practice