[Python] [python hallenge] [TBC] The old Python online challenge is very interesting (C0-C4)

Keywords: Python PHP Lambda

Estimated reading time: 15 minutes

Background: It's interesting to discover by chance when searching for information. Every level covers a lot of knowledge points.

Python version: 3.0

Talking is cheap,show me the code

Home page: http://www.pythonchallenge.com/

Warm-up Pass: Click Start Challenge to Enter Warm-up Pass

http://www.pythonchallenge.com/pc/def/0.html

1. According to the prompt, enter 238.html

2. Get a new hint: No... the 38 is a little bit above the 2...

3. Re-view the picture and enter it http://www.pythonchallenge.com/pc/def/274877906944.html

1 bogon:~ hbai$ python
2 Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
3 [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> 2**38
6 274877906944
7 >>> 

4. Congratulations on formally entering the First Pass

First pass: http://www.pythonchallenge.com/pc/def/map.html

What about making trans?

 1 #coding=utf-8
 2 
 3 #In py2.7 need following import
 4 #from string import maketrans
 5 
 6 
 7 #page: http://www.pythonchallenge.com/pc/def/map.html
 8 #Trial 1: Replace the specified three characters and find that the sentence is still incomprehensible
 9 #k ->  M  O->Q  E > G
10 str = "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."
11 
12 print(str.replace('k','m').replace('o','q').replace('e','g'))
13 
14 #According to the prompt, use transtab translate
15 intab = "abcdefghijklmnopqrstuvwxyz"
16 outtab = "cdefghijklmnopqrstuvwxyzab"
17 trantab = str.maketrans(intab, outtab)
18 
19 print(str.translate(trantab))
20 
21 #http://www.pythonchallenge.com/pc/def/map.html
22 print('http://www.pythonchallenge.com/pc/def/'+ 'map'.translate(trantab) + '.html')

Use maketrans, translate to translate, pass the customs

 

The second pass: http://www.pythonchallenge.com/pc/def/ocr.html

View the source code of the web page according to the prompt

<!--
find rare characters in the mess below:
-->

<!--
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
@##&{# 
. . . .
-->

Goal: Find the fewest characters
Copying strings to local storage is slow, but eventually the answer is equal.
def check_CharFrequence(str):
    decode = []
    for i in str:
        if str.count(i) < 5:
            decode.append(i)
    print(''.join(decode))
    #print sorted(char_freq.items(),key = lambda x: (x[1]))   #    aeilquty

with open('C2_info.txt') as f:
    #My method: it's very very not good, because of N^N complex
    check_CharFrequence(f.read())
    print(f.read())

Further Reflections: See the Standard Answers page http://www.pythonchallenge.com/pcc/def/equality.html

 

The third level: http://www.pythonchallenge.com/pc/def/equality.html

According to tips: One small letter, surrounded by EXACTLY three big body guards on each of its sides.

Write a rule for the first time: # pattern = re.compile('[A-Z]{3}([a-z])[A-Z]{3}',re.S), save the source code and run, but found that it is still wrong.

Later, referring to the answer, I found that it should be revised as follows

#coding=utf-8
import re
#page= http://www.pythonchallenge.com/pc/def/equality.html
#Previous std answer: http://www.pythonchallenge.com/pcc/def/equality.html
#Current page is http://www.pythonchallenge.com/pcc/def/linkedlist.php

sampleStr='kAewtloYgcFQaJNhHVGxXDiQmzjfcpYbzxlWrVcqsmUbCunkfxZWDZjUZMiGqhRRiUvGmYmvnJIHEmbT \
MUKLECKdCthezSYBpIElRnZugFAxDRtQPpyeCBgBfaRVvvguRXLvkAdLOeCKxsDUvBBCwdpMMWmuELeG \
ENihrpCLhujoBqPRDPvfzcwadMMMbkmkzCCzoTPfbRlzBqMblmxTxNniNoCufprWXxgHZpldkoLCrHJq \
vYuyJFCZtqXLhWiYzOXeglkzhVJIWmeUySGuFVmLTCyMshQtvZpPwuIbOHNoBauwvuJYCmqznOBgByPw'

'''Hint: One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. '''
#My method is NOT correct
#pattern = re.compile('[A-Z]{3}([a-z])[A-Z]{3}',re.S)

#Following is CORRECT
pattern = re.compile('[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]')
#print pattern.findall(sampleStr)


with open('C3_info.txt') as f:
    codeList = pattern.findall(f.read())
    print(''.join(codeList))

The fourth pass: http://www.pythonchallenge.com/pc/def/linkedlist.php

As a rule, look at the source code and find the following tips:

<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>

Try opening the page http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

Interesting. You need to climb up to the 300th floor in turn to win.

 1 # coding=utf-8
 2 
 3 # page = http://www.pythonchallenge.com/pc/def/linkedlist.php
 4 
 5 page = 'http://www.pythonchallenge.com/pc/def/linkedlist.php'
 6 loopMainpage = 'http://www.pythonchallenge.com/pc/def/'
 7 firstpage = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
 8 
 9 from urllib import request
10 import time
11 import re
12 
13 
14 def looppage(page,num):
15     response = request.urlopen(page+str(num))
16     html = response.read()
17     print(html.decode("utf-8"))
18     pattern = re.compile('and the next nothing is (\d{1,10}).*?')
19     target = re.findall(pattern,html.decode("utf-8"))
20     print(page + target[0])
21     return target[0]
22 
23 
24 import random
25 return_num = looppage(firstpage,'82682')
26 i = 0
27 while i < 300:
28     print('Index %s:' % i)
29     return_num = looppage(firstpage,return_num)
30     time.sleep(random.randint(5,10))
31     i +=1

Pits encountered during climbing:

1. Pages sometimes don't respond, so a random waiting time is added. (Actually, it's safest to crawl randomly with an anonymous agent, but I haven't finished that method yet.)

2. There is a layer that prompts you to divide the current number by two, so you have to manually enter it again and again to continue climbing.

To the end, congratulations: peak.html

Index 107:
and the next nothing is 52899
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=52899
Index 108:
and the next nothing is 66831
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=66831
Index 109:
peak.html
Traceback (most recent call last):
  File "/Users/hbai/PycharmProjects/interview/Py_study/pythonchallenge/C4.py", line 57, in <module>
    return_num = looppage(firstpage,return_num)
  File "/Users/hbai/PycharmProjects/interview/Py_study/pythonchallenge/C4.py", line 32, in looppage
    print(page + target[0])
IndexError: list index out of range

Process finished with exit code 1

The fifth pass: http://www.pythonchallenge.com/pc/def/peak.html

Online prompts should be operated using pickle library, tried unsuccessfully, and then continue when you have time.

To Be Continued...

Posted by greenie__ on Sat, 05 Jan 2019 20:06:11 -0800