Python Challenge Customs Tips (1)

Source: Internet
Author: User
Tags ord rfc

Formally starting 1th, the special part of this off URL is map.

In this picture there is a book, which says K→m,o→q,e→g, a little thought can be found that the letters are in alphabetical order to move backwards two bits, then the most opportunistic way is to map the 3 letters according to this rule to change the line. Although this is the result that I want, but is not I want the process, still have to follow the normal programming method, and the following there are other tips, let's read all the first.

There is a line below the picture: Think twice and then solve. Below is a few lines of irregular letter combinations, which are clearly to be transformed by the above rules.

For such conversions, it is natural to think of ASCII code. After reviewing the data, we found that the ASCII code and the characters in Python are converted using the Ord () function and the Chr () function. Then note that Y and Z should be converted to A and B, then these two move forward 24 bits. After writing the program to find a problem, because I used to record the conversion content is a list table, if the direct print is not conducive to reading, continue to consult the data after the discovery join () function can implement list to string conversion, so the official code is as follows:

1Code ='g FMNC WMS Bgblr RPYLQJYRC gr ZW fylb. Rfyrq ufyr amknsrcpq ypc dmp.2 bmgle GR GL ZW fylb GQ glcddgagclr ylb rfyrq UFW rfgq rcvr GQ QM Jmle.3 sqgle QRPGLE.KYICRPYLQ () GQ PCAMKKCLBCB. LMU ynnjw ml RFC spj.'4 5decode = []6  forIinchCode:7     ifOrd'a') <= Ord (i) <= Ord ('x'):8Decode.append (Chr (ord (i) + 2))9     elifOrd'y') <= Ord (i) <= Ord ('Z'):TenDecode.append (Chr (Ord (i)-24)) One     Else: A decode.append (i) - Print("'. Join (Decode))
View Code

After running the output after the conversion prompt: I hope you didnt translate it by hand. Thats what computers was for.doing it in by hand was inefficient and thats why this text was so long.using String.maketrans ( ) is recommended. Now apply to the URL.

It is good to finish the step by step, the hint mentions the function of String.maketrans (). According to the official Python documentation, the String.maketrans (from,to) function gives the translate () function a translation table from the from map to the to, while translate (S,table,[deletechars]) The function is to remove characters from s that appear in the Deletechars (if any), and then convert them according to the rules of the table.

The code for using Maketrans () and translate () under string is as follows:

1 Importstring2 3Code ='g FMNC WMS Bgblr RPYLQJYRC gr ZW fylb. Rfyrq ufyr amknsrcpq ypc dmp.4 bmgle GR GL ZW fylb GQ glcddgagclr ylb rfyrq UFW rfgq rcvr GQ QM Jmle.5 sqgle QRPGLE.KYICRPYLQ () GQ PCAMKKCLBCB. LMU ynnjw ml RFC spj.'6 7 Print(String.translate (Code, String.maketrans ('abcdefghijklmnopqrstuvwxyz','Cdefghijklmnopqrstuvwxyzab')))
View Code

In the 2nd left-hand corner of the prompt, the URL of the PC to PCC can see the previous level of various solutions. It is a lot to open a look, but also includes the solution of other languages. It is worth mentioning that a solution that uses string.ascii_lowercase and string.ascii_lowercase[2:] + string.ascii_lowercase[:2] is written in place of my Code ' Abcdefghijklmnopqrstuvwxyz ' and ' cdefghijklmnopqrstuvwxyzab ' look messy and error-prone. First , the string.ascii_lowercase here is a string that combines all lowercase letters from a to Z, Of course, this can also be written in string.lowercase, in most cases the same, but in some cases the latter will change, as well as String.ascii_uppercase and string.uppercase. Second, the slice in Python is used here, and the concept of slicing comes into contact when you look at the basic concept of Python, but there is no chance to use it, just to take this opportunity to solidify it. For example String.ascii_lowercase[2:] is starting from string.ascii_lowercase[2] Slice to the end of the string, that is, from ' C ' to ' Z ', and String.ascii_lowercase[:2] is to slice from the beginning of the string to the 2nd position, that is, ' ab ', if there is a string.ascii_lowercase[3:6], then it should be ' def '.

Finally, the map translation is written into the code:

1 Import string 2 ' Map '. Translate (String.maketrans (String.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase [: 2]))
View Code

Got the keyword into the next level:OCR.

Python Challenge Customs Tips (1)

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.