Python 12 level challenge, see how many levels you can pass

Level 0

Calculate 2 to the 38th power

>>> 2**38
274877906944
>>> pow(2,38)
274877906944

Level 1

Implement letter shift codec

import string, sys
a = string.ascii_lowercase
b = a[2:] + a[:2]
s = sys.stdin.read()
print s.translate(string.maketrans(a, b))

Level 2

Find characters that have only appeared once from a large string of strings and output them in the original order.

import urllib, re, sys
def get_file(fname):
    url = 'http://www.pythonchallenge.com/pc/def/' + fname
    return urllib.urlopen(url).read()
dat = get_file('ocr.html')
txt = re.findall(r'<!--(.*?)-->', dat, re.S)[-1]
d = {
    
    }
for i in txt:
    d[i] = d.get(i, 0) + 1
for i in txt:
    if d[i] == 1:
        sys.stdout.write(i)

Level 3

Find the lowercase letters from a large section of characters, and require exactly three uppercase letters on the left and right of it, and output them in the original order.

import urllib, re, sys
def get_file(fname):
    url = 'http://www.pythonchallenge.com/pc/def/' + fname
    return urllib.urlopen(url).read()
dat = get_file('equality.html')
txt = re.findall(r'<!--(.*?)-->', dat, re.S)[-1]
ans = re.findall(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', txt, re.S)
print ''.join(ans)

Level 4

There is a group of web pages similar to the linked list structure, each page gives the address parameters of the next page, traverse the entire chain to find the last node.

import urllib, re
def get_file(fname):
    url = 'http://www.pythonchallenge.com/pc/def/' + fname
    return urllib.urlopen(url).read()
pre = 'linkedlist.php?nothing='
idx = '12345'
while True:
    dat = get_file(pre + idx)
    print dat
    ans = re.search(r'\d+', dat)
    if ans is None: break
    idx = ans.group(0)

Level 5

Use the pickle module to load the serialized content and print it out.

import urllib, pickle
def get_file(fname):
    url = 'http://www.pythonchallenge.com/pc/def/' + fname
    return urllib.urlopen(url).read()
dat = get_file('banner.p')
ans = pickle.loads(dat)
print '\n'.join([''.join([j[0] * j[1] for j in i]) for i in ans])

Level 6

Similar to level 4, but this time the node information exists in a zip file, and the annotation content of each file needs to be spliced ​​from beginning to end.

import urllib, StringIO, zipfile, re
def get_zip(fname):
    url = 'http://www.pythonchallenge.com/pc/def/' + fname
    return zipfile.ZipFile(StringIO.StringIO(urllib.urlopen(url).read()))
z = get_zip('channel.zip')
idx = 'readme'
cmt = ''
while True:
    cmt += z.getinfo(idx + '.txt').comment
    txt = z.read(idx + '.txt')
    ret = re.search(r'\d{2,}', txt)
    if ret is None: break
    idx = ret.group(0)
print cmt

Level 7

Take a part of the pixels in the middle of a picture and convert the gray value into the corresponding Ascii characters.

>>> import urllib, StringIO
>>> from PIL import Image
>>> src = urllib.urlopen('http://www.pythonchallenge.com/pc/def/oxygen.png').read()
>>> img = Image.open(StringIO.StringIO(src))
>>> w, h = img.size
>>> ''.join([chr(img.getpixel((i, h/2))[0]) for i in xrange(0, w, 7)])
'smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]pe_'
>>> ''.join(map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121]))
'integrity'

Level 8

Give the user name and password content compressed by bzip and restore them.

>>> import bz2
>>> uid='BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
>>> pwd='BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'
>>> bz2.BZ2Decompressor().decompress(uid)
'huge'
>>> bz2.BZ2Decompressor().decompress(pwd)
'file'

Level 9

Given a list of integers representing a line, the x and y coordinate subscripts of the i-th point are 2i and 2i+1 respectively, and draw the graph according to the coordinate points.

>>> from PIL import Image, ImageDraw
>>> l1 = [146,399, ...]
>>> l2 = [156,141, ...]
>>> img = Image.new('1', (500,500), 1)
>>> draw = ImageDraw.Draw(img)
>>> draw.line(zip(l1[0::2], l1[1::2]))
>>> draw.line(zip(l2[0::2], l2[1::2]))
>>> img.save('out.png')

Level 10

Find the length of item 31 in the look-and-say sequence.

a = ['1']
for i in xrange(30):
    t = ''
    n = 0
    x = ''
    for i in a[-1]:
        if n == 0:
            n = 1
            x = i
        elif i == x:
            n += 1
        else:
            t += str(n) + x
            n = 1
            x = i
    else:
        t += str(n) + x
    a.append(t)
print len(a[30])

Level 11

Given an image, it is required to remove the pixels whose x and y coordinates are an odd number.

import urllib
import StringIO
from PIL import Image, ImageDraw
url = 'http://www.pythonchallenge.com/pc/return/cave.jpg'
dat = urllib.urlopen(url).read()
img = Image.open(StringIO.StringIO(dat))
w, h = img.size
for i in xrange(w):
    for j in xrange(h):
        if (i + j) % 2:
            img.putpixel((i,j), 0)
img.save('out.png')

Resource portal

  • Pay attention to [ Be a tender program ape ] public account
  • Reply to the [ python information ] [ 2020 Autumn Recruitment ] in the background of [ Be a tender program ape ] public account to get the corresponding surprise!

「❤️ Thank you everyone」

  • Like to support it, so that more people can see this content (If you don’t like it, it’s all hooligans-_-)
  • Welcome to share your thoughts with me in the message area, and you are welcome to record your thought process in the message area.

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/108761520