Pythonchallenge_1
First, the experimental explanation
The following introduction is the experimental building default environment, if you are using a custom environment, please change to your own environment introduction.
1. Environment Login
No password automatic login, system user name Shiyanlou, password Shiyanlou
2. Introduction to the Environment
This experiment environment uses the Ubuntu Linux environment with the desktop, the experiment will use the program on the desktop:
1. LX Terminal (lxterminal): Linux command line terminal, Open will enter the bash environment, you can use the Linux command
2. Firefox: Browser, can be used in the need for the front-end interface of the course, only need to open the environment to write the Html/js page can be
3. GVim: Very useful editor, the simplest usage can refer to the course Vim editor
3. Use of the environment
Use the Gvim editor to enter the code and files required for the experiment, and use the LX Terminal (lxterminal) to run the required commands.
After completing the experiment, you can click "Experiment" above the desktop to save and share the results to Weibo to show your friends the progress of your study. The lab building provides a back-end system that can prove to be true that you have completed the experiment.
The Experiment records page can be viewed in the My Home page, which contains each experiment and notes, as well as the effective learning time of each experiment (refers to the time of the experiment desktop operation, if there is no action, the system will be recorded as Daze time). These are the proof of authenticity of your studies.
Ii. introduction of the course
The ' Pythonchallenge ' series A total of 11 projects, continuous updating, each project will be detailed in the course of 3 ' pythonchallenge ' customs clearance issues and different solutions, after-school exercises in the layout of a task problem, The topic will be unveiled in the next project.
This series of topics belong to the online problem, because the experimental building temporarily does not provide access to the network, so please verify the answer when trouble click your browser to visit the Web page to verify the answer.
All topics and Reference Solutions Copyright Pythonchallenge Official website, curriculum preparation belongs to the original experimental building, welcome to ask questions in the question-and-answer area, the small series will actively answer, also welcome in the comment area of the Spit Groove ~
Iii. contents of the experiment
3.1 warming up
Problem: Try to change the ' URL ' address according to the slice:http://www.pythonchallenge.com/pc/def/0.html.
* * Small brain hole: * * is greeted by three numbers, look like 2 of the 38-time side, the result is this? Try to figure out the results in the Python interactive environment ~
Quick fix, see if it's ' 274877906944 '? (Change the Last child link ' 0 ' in the ' URL ' address.) )
So easy, so magical and wooden? Go to the next http://www.pythonchallenge.com/pc/def/map.html. Well, really warm-up exercise, is not too underestimated your IQ? It doesn't matter, let's play
# # # 3.2 How to convert?
Question: Attention, please consider the following string:
' G FMNC WMS Bgblr RPYLQJYRC gr ZW fylb. RFYRQ Ufyr AMKNSRCPQ ypc DMP. Bmgle GR GL ZW fylb GQ glcddgagclr ylb rfyr ' q UFW rfgq rcvr GQ QM Jmle. Sqgle QRPGLE.KYICRPYLQ () GQ PCAMKKCLBCB. LMU ynnjw ml RFC spj. '
* * Small brain hole: * *' k->m ', ' 0->q ', ' e->g ' These three conversions are separated by a character, is ' L ', ' P ', ' F '; is that the above-mentioned chaotic string conversion rule is the ' ASCII code ' of each original character Plus 2?
First, the above words are Fugazai to the text test_1.py in the experimental building environment through the shearing Board:
1 """ g FMNC WMS Bgblr RPYLQJYRC gr ZW fylb. Rfyrq ufyr amknsrcpq ypc dmp. Bmgle GR GL ZW fylb GQ glcddgagclr ylb rfyr ' q UFW RFGQ RCVR GQ QM Jmle. Sqgle QRPGLE.KYICRPYLQ () GQ PCAMKKCLBCB. LMU ynnjw ml RFC spj. """
Next, how do you convert the letters to ' ASCII code '? ' Python ' provides the function ' ord ' to convert the letter to ' ASCII ', providing the function ' CHR ' to convert the corresponding ' ASCII ' to a letter, then the problem is simple, just loop through the characters in the string, and use the above function to convert the letters:
1o =""2 3 foreachinchText:4 5 ifOrd (each) >= Ord ('a') andOrd (each) <= Ord ('Z'):6 7O + = Chr (ord (each) + 2-ord ('a'))% + ord ('a'))#try to understand the conversion formula8 9 Else:Ten OneO + =x A - PrintO
Look at the results of the output:
By the answer given to know (need to have level four English, not just online translation) The above method of the ' URL ' link in the ' map ' can be converted.
* * Standard ANSWER * *
The author recommends using the function ' String.maketrans () ', so what does this function do?
In the ' Python ' interactive window type:
1 Import string 2 3 Help (String.maketrans)
Output function Documentation:
A lot of English, understand the following information (keyboard input ' Q ' to launch the document):
The function has two arguments ' frm ' and ' to ', and the two parameters must have the same length, and the function returns a conversion formula ' table '.
Corresponding to this function is the function ' string.translate ', you can try to view the function document as above, the function ' string.translate ' has two parameters, namely the string ' s ' and the conversion formula ' table ', Returns the string ' s ' that is converted by the conversion formula.
So theory, you may not understand, directly modify the original file ' test_1.py ', add the code (note the original method commented out, or just like me to package each solution as a function):
1 Importstring2 3 4 5 defstd_solution (text):6 7Table =String.maketrans (8 9 String.ascii_lowercase,Ten OneString.ascii_lowercase[2:] + string.ascii_lowercase[:2]) A - Printstring.translate (text, table) - the - - if __name__=='__main__': - +Std_solution (text)
Look carefully at the ' String.maketrans () ' inside of the two parameters, where ' string.lowercase ' refers to the lowercase 26 English letters ' A-Z ', the second parameter is to convert the format ' c-z ' + ' ab ', that is, the method of moving each character back two bits, Therefore, the ' text ' string can be converted directly using the method ' table ', which is a well-defined method.
Then the answer is obvious, replace the above ' text ' content with the ' map ' character in the web link, output as ' OCR ', try to open http://www.pythonchallenge.com/pc/def/ocr.html to find the next question of customs clearance.
3.3 Character Recognition
Problem: Identify the strings, they may be in the book, or in the source code
* * Small series of brain hole: * * The book is basically not see what information, even if there is also a needle in the haystack, since said is in the source code, then we can right-click "Review element" to see the source, so in this place:
Below, a bunch of seemingly irregular strings are found:
Note that it has a reminder: find rare characters from a messy string below. This is the problem we really want to solve!
Because the experimental building environment can not access the network, the small series has been the chaos of the characters uploaded to the experimental building environment, please use the following command to download the file:
1 wget http://labfile.oss.aliyuncs.com/courses/408/mess.txt
Find the minimum character, since it is a character, then it is not a normal symbol, just try to find the character:
1 Importstring2 3 4 5Text = open ('Mess.txt'). Read ()#Open File Read string6 7 8 9 defmy_solution (text):Ten One """Select characters from text that belong to the English alphabet""" A - - thes = Filter (LambdaX:xinchstring.letters, text) - - Prints - + - + if __name__=='__main__': A atMy_solution (text)
Operation Result:
The next level of the URL is obviously the ' equality ' into.
For the function ' filter ', please use the ' help ' command to view the function document, where the first parameter of ' filter ' is a ' lambda ' function, and the second argument is the string sequence ' text '.
For the use of ' lambda ', see the following example:
Here's the ' lambda ' function that accomplishes the same thing as the above normal function ' F (x) '. Note the short syntax here: there is no parentheses around the argument list, and the ' return ' keyword is omitted (implied, because the entire function has only one row). Moreover, the function does not have a function name, but it can be assigned to a variable such as ' G ' for invocation.
* * Standard ANSWER * *
The minimum number of occurrences is equivalent to the frequency of occurrence less than the average of the character
We want to find the fewest occurrences of characters from a lot of confusing characters, the trick is to consider characters that appear below average in the text ' Mess.txt ', and the only information we have is that the characters are non-frequent and are compatible with the ' URL ' type (for example, carriage return symbols and whitespace are no longer considered).
The algorithm is as follows:
1. Read the text content;
2. Define a dictionary that stores each character and the corresponding frequency of occurrence;
3. Calculate the average rarity of characters (all characters/number of characters);
4. Cyclic output of characters less than the average level of rarity.
1s ="'. Join ([Line.rstrip () forLineinchOpen'Mess.txt')]) 2 3OCC = {}4 5 6 7 forCinchS:8 9OCC[C] = Occ.get (c, 0) + 1#The same character, the value of the dictionary plus 1Ten OneAVGOC = Len (s)//Len (OCC) A - - the Print "'. join ([C forCinchSifOCCURRENCES[C] < AVGOC])
It should be noted here that the value of the reference dictionary is best used ' occ.get (c) ' instead of ' occ[c ', otherwise it is easy to appear ' Keyerror ', as for the reasons, please think about it and use Baidu and other search engines to inquire and understand relevant knowledge.
* * Editor's comments: * * Although the official answer refers to the larger dictionary, but the "rare situation" This evaluation index is not very good, does not conform to the average person's evaluation criteria, General people's evaluation is generally statistical significance, such as the frequency of occurrence.
Then someone is more clever to seize the word "rare", that is, the number of characters is rare, then assume that the number of occurrences of the character is ' 1 '?
* * "opportunistic" solution * *
In fact, the solution is so rich, the brain hole is so big; Do you ever think of a more ' pythonic ' way of getting that messy string of strings to get the source code directly from ' Python ' and select that pile of strings to store directly? (left to everyone as an extracurricular exercise).
Iv. Task_1
So, have you ever been tempted to introduce so many solutions? Is it because of the law of finding this series of questions? Then give you a chance to solve the following problems:
Question: As shown, the answer to the next question: the middle is a lowercase letter, each side has three uppercase letters to do bodyguard!
* * Tips: * * to the next question of the word format is according to the above questions given the same, the other character source can refer to the above topics given the problem-solving ideas Oh! (I'm not so obvious ~) in addition, not necessarily in the ' Python ' language to solve this problem, you can use your familiar language to solve.
Pythonchallenge (i)