欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pythonchallenge解法源码(1-5)

程序员文章站 2024-02-12 18:12:34
...

今天看到一个好玩的游戏,搜了一下,很早的一个项目了,想着这样去熟悉一下python的一些用法,下面把每一关的源码贴出来!

第一关

import string

text = '''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.'''


def translate_method1():
    text_translate = ''
    for i in text:
        if str.isalpha(i): #检测字符串是否只由字母组成
            n = ord(i)   #返回对应字符的十进制数
            if i >= 'y':
                n = ord(i) + 2 - 26
            else:
                n = ord(i) + 2
            text_translate += chr(n)  #将十进制转换成字符
        else:
            text_translate += i

    print(text_translate)

def translate_method2():
    l = string.ascii_lowercase
    t = str.maketrans(l,l[2:] + l[:2]) #创建字符映射表,前者是字符串,后者是转换目标
    print(text.translate(t))

if  __name__== "__main__":
    translate_method1()
    translate_method2()

第二关

import requests
import re

url = 'http://www.pythonchallenge.com/pc/def/ocr.html'

def parse(url):
    response = requests.get(url).text
    pattern = '.*?<!--.*?-->.*<!--(.*?)-->'
    text = re.findall(pattern,response,re.S)
    return text

def sum(txt):
    str = ''.join(txt)
    lst = []
    key = []
    for i in str:
        lst.append(i)
        if i not in key:
            key.append(i)

    for items in key:
        print(items,lst.count(items))

if __name__ == "__main__":
    txt = parse(url)
    sum(txt)

第三关

import requests
import re

url = 'http://www.pythonchallenge.com/pc/def/equality.html'

def parse(url):
    response = requests.get(url).text
    pattern = '<!--(.*?)-->'
    text = re.findall(pattern,response,re.S)
    return text

def parse_text(txt):
    pattern = '[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]'
    res = re.findall(pattern,str(txt),re.S)
    print(res)
    #res = re.compile(pattern)
    #print(''.join(res.findall(str(txt))))

if __name__ == "__main__":
    txt = parse(url=url)
    parse_text(txt)

第四关

import requests
import re


start_url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=63579'

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='

resp = requests.get(start_url).text

count = 0

while True:
    try:
        nextid = re.search('\d+',resp).group()
        count += 1
        nextid = int(nextid)
    except:
        print('the last url is :' + nexturl)
        break

    nexturl = url + str(nextid)
    print('url %s:%s' %(count,nexturl))
    resp = requests.get(nexturl).text

第五关

import requests
import urllib
import pickle
import re

base_url = "http://www.pythonchallenge.com/pc/def/"
url = "http://www.pythonchallenge.com/pc/def/peak.html"

response = requests.get(url).text
pattern = 'src="(.*?)"'
text = re.findall(pattern,response,re.S)
url = url + str(text[1])
urllib.request.urlretrieve(base_url + text[1],filename='banner.p')

with open('banner.p','rb') as fp:
    data = pickle.load(fp)
    print(data)
    result = ""
    for line in data:
        print("".join(x[0]*x[1] for x in line))

 

相关标签: pythonchallenge