python challenge

level 0

1
print(2**38)

level 1

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.

字符串替换:k-m o-q e-g
我以为只是简单的替换,看了答案发现是找规律-> 移位加密

1
2
3
4
5
6
7
8
import string

s = """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."""
l = string.ascii_lowercase
table = str.maketrans(l, l[2:]+l[:2])
print(s.translate(table))

print("map".translate(table)) # 得到

level 2

1
2
3
4
5
6
7
8
9
10
f = open('tmp.txt', 'r')
s = f.read()
char = []

for c in s:
if c in char:
pass
else:
char.append(c)
print(c, s.count(c)) # 得到equality

level 3

1
2
3
4
5
6
7
8
9
import requests
import re

r = requests.get('http://www.pythonchallenge.com/pc/def/equality.html')
t = re.findall('(?<=<!--)([\w\W\r\n]*?)(?=-->)', r.text)[0] #emmm... 这里不太明白?

l = re.findall('[a-z]{1}[A-Z]{3}([a-z]{1})[A-Z]{3}[a-z]{1}', t) # 需要xXXXxXXXXx这种才可以

print(''.join(l)) # 得到linkedlist

level 4

1
2
3
4
5
6
7
8
9
10
11
12
import requests
import re

num = 12345
url = "http://www.pythonchallenge.com/pc/def/linkedlist.php"
params = {"nothing":num}

for i in range(400):
r = requests.get(url=url, params=params)
num = ''.join(re.findall("[0-9]", r.text))
params = {"nothing":num}
print(r.url)

peak.html

level 5

pick hell-> pickle

关于pickle
基础方法:dump和load vs dumps和loads(前者是写入文件,后者是直接返回)
包含两个类:Pickler and Unpickler(如果想进行更多的控制,可以创建这两个对象)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
import re
import pickle

r = requests.get("http://www.pythonchallenge.com/pc/def/banner.p")
data = pickle.loads(bytes(r.text, encoding='utf-8'))
str = ""

for lst in data:
# print(lst)
for i in lst:
str+=i[0]*i[1]
str+="\n"
print(str) # 得到channel

channel.html

level 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import re
from zipfile import ZipFile
# Next nothing is 94191 90052

zipPath = "/Users/m3lon/Downloads/channel.zip"
num = "90052"
comment = ""
with ZipFile(zipPath) as zip:
while True:
content = zip.read(num+".txt").decode('utf-8')
comment += zip.getinfo(num+".txt").comment.decode('utf-8')
print(content)
match = re.search('Next nothing is (\d+)', content)
if match == None:
break
num = match.group(1)
print(comment) # 得到 hockey

扩展:
bytes是一个类,https://docs.python.org/3/library/stdtypes.html#bytes
这个类有一个方法,bytes.decode(encoding=”utf-8”, errors=”strict”)
两个逆过程:bytes.decode(‘utf-8’) 和 str.encode(‘utf-8’)

访问hockey.html,得到it’s in the air,look at the letters,暗示看图片中的字母,得到oxygen

level 7

IO.BytesIO 内存中字节缓冲流的实现:https://docs.python.org/3/library/io.html#io.BytesIO
很多时候,数据读写不一定是文件,也可以从内存中读写,StringIO和BytesIO就是从内存中读写str和bytes
pillow PIL moudle中的图片处理模块PIL.Image https://pillow.readthedocs.io/en/stable/reference/Image.html
requests的几种response: 主要是r.text和r.content的区别,text是使用r.encoding解码后的,content是字节流
列表推导 [(x, y) for x in range(3) for y in range(3)]

1
2
3
4
5
6
7
8
9
10
11
12
13
from PIL import Image 
from io import BytesIO
import requests

pixel = []
img = Image.open(BytesIO(requests.get("http://www.pythonchallenge.com/pc/def/oxygen.png").content))
for x in range(img.width):
pixel.append(img.getpixel((x,img.height/2)))
pixel = pixel[::7]
ords = [r for r,g,b,a in pixel if r==g==b]
print(''.join(map(chr, ords))) # smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]
ords = [105, 110, 116, 101, 103, 114, 105, 116, 121]
print(''.join(map(chr, ords))) # integrity

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

level 8

最近经常遇到TypeError: a bytes-like object is required, not ‘str’这种TypeError的报错,对于传入参数,参数类型这种报错,最简单的方法就是如下面在前面加上b表明为字节类型即可

1
2
3
4
5
6
import bz2
un = b'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw = b'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'

print(bz2.decompress(un))# huge
print(bz2.decompress(pw))# file

level 9

这道题主要学习了一下PIL模块下的ImageDraw类,Image负责图片的open show等基本功能,ImageDraw负责图片的绘制,可以绘制各种图形,此题绘制的是多边形polygon,每个类各司其职

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from PIL import Image,ImageDraw

first=[146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,
310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,
190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,
389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,
215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,
290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,
279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,
327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,
328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,
259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,
352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,
120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,
214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,
102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,
113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,
133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,
111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,
332,155,348,156,353,153,366,149,379,147,394,146,399]

second=[156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,
157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,
125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,
77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,
158,121,157,128,156,134,157,136,156,136]

img = Image.new('RGB', (500,500), 0)
draw = ImageDraw.Draw(img)
draw.polygon(first)
draw.polygon(second)
img.show()

输入cow.html,提示公牛,bull.html

level 10

look and say sequence:1,11,21,1211,111221…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def get_next(s):
num = []
l = 1
for i in range(len(s)):
try:
if s[i] == s[i+1]:
l += 1
else:
num.extend([str(l), s[i]])
l = 1
except:
num.extend([str(l), s[i]])

return "".join(num)

a = ['1']

while len(a) <= 30:
a.append(get_next(a[-1]))

print(len(a[30]))

坚持原创技术分享,您的支持将鼓励我继续创作!