Byte of Python Learning Note (2)--palindrome practice

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.