您的位置:首页 > 编程语言 > Python开发

Python challenge

2016-05-02 20:45 585 查看
python练习题:python challenge

level 0

print 2**38


answer:274877906944(用这个数字替换掉URL的0)

level 1

# s = raw_input()
# s = list(s)
# for i in range(len(s)):
#     if 'a' <= s[i] <= 'z':
#         num = ord(s[i]) + 2
#         if num > ord('z'): s[i] = chr(ord('a') + num - ord('z') - 1)
#         else: s[i] = chr(num)
#     if 'A' <= s[i] <= 'Z':
#         num = ord(s[i]) + 2
#         if num > ord('Z'): s[i] = chr(ord('A') + num - ord('Z') - 1)
#         else: s[i] = chr(num)
# print ''.join(s)

from string import maketrans
s = raw_input()
upper = []
low = []
for i in range(24):
low.append(chr(ord('a') + i + 2))
upper.append(chr(ord('A') + i + 2))
for i in range(2):
low.append(chr(ord('a') + i))
upper.append(chr(ord('A') + i))
change = ''.join(low + upper)
upper = maketrans('', '')[65:91]
low = maketrans('', '')[97:123]
source = low + upper
print s.translate(maketrans(source, change))


answer:ocr(map的映射)

level 2

解析:

根据提示:recognize the characters. maybe they are in the book, but MAYBE they are in the page source.
查看网页源代码,知道这个题是要你从给出的一大块混乱的字符中找出其中的稀少字符。


代码:

# def cmp(x, y):
#     if x[1] > y[1]: return 1
#     elif x[1] < y[1]: return -1
#     else: return 0
#
# File = open('data.txt', 'r')
# dic = {}
# for line in File:
#     for c in line:
#         if c in dic: dic[c] = dic[c] + 1
#         else: dic[c] = 0
# char_num = dic.items()
# char_num.sort(cmp)
# for i in range(len(char_num)):
#     print char_num[i][0], char_num[i][1]
# s = ""
# for i in range(8):
#     s+= char_num[i][0]
# print s

try:
File = open('data.txt', 'r')
rare_char = ""
while True:
char = File.read(1)
if char.isalpha(): rare_char += char
if not char: break
finally:
File.close()
print rare_char


answer: equality

level 3

找出被左右刚好都是三个大写字母的小写字母,正则表达式很容易求解


import re

ans = ''
pattern = re.compile('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]')
try:
f = open('data.txt', 'r')
cnt = 0
for line in f:
cnt += 1
result = re.search(pattern, line)
if result:
ans += result.group(1)
print cnt
finally:
f.close
print ans


ans: linkedlist (这里有个坑,直接往URL上用linkedlist会得到一个空白页面,上面只有一行字母linkedlist.php,不是答案错误,是要把html改成php才可以过)

level 4

点击图片,进入新页面,每一页的url中都有一个nothing值,并且当前页的内容中会说明下一页的nothing值。只需要不断用下一页的nothing值替换当前页的nothing即可找到最终结果。


import re
from urllib import urlopen

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
nothing = '12345'
pattern = re.compile('[0-9]+')
while nothing.isdigit():
if nothing == '16044': nothing = '8022'
if nothing == '82683': nothing = '63579'
text = urlopen(url+nothing).read()
result = re.search(pattern, text)
if result: nothing = result.group(0)
print nothing


ans: peak.html

level 5

根据所给出的信息以及查看源码的banner.p链接中所给出的信息,我也联想不到pickle模块。
事实就是用pickle模块对banner.p中的信息解序列化,得到一个列表,对列表中的元素输出,可得到一个由字符组成的像channel的图形。


import re, pickle
from urllib import urlopen

url = 'http://www.pythonchallenge.com/pc/def/banner.p'
text = urlopen(url).read()
rel = pickle.loads(text)
for list in rel:
print ''.join(t[0]*t[1] for t in list)


answer: channel

level 6

这个题真的难 0.0

步骤:

1. 还是像往常一样查看了网页源代码

2. 注意到第一行,然后用zip替换了html,之后自动下载了一个名为channel.zip的文件

3. 查看了readme.txt,也是像第4题一样,通过给的nothing,不断进入文件。

4. 但是,最终并没有给出答案,而是给出了提示“Collect the comments”

5. 懵逼了。搜了一下答案,才知道,可以通过zipfile模块来处理zip文件。下面给出几个有关这个题的类和函数:

class zipfile.ZipFile(file [,mode [,compression [,allowZip64] ] ])
ZipFile类构造函数,打开一个zip文件,并返回该zip文件的ZipFile对象。
* file:要打开的zip文件的文件名,可以是一个普通的文件名(一个string),或者是一个类文件(file-like)的对象。
* mode:文件打开方式。’r’表示读取,’w’写一个新文件, ‘a’表示追加到一个已存在的文件后面。

ZipFile.read(name [,pwd])
返回压缩文件中name文件的字节(bytes)。压缩文件必须是以read或者append模式打开。
* name:文件名或ZipInfo对象
* pwd:解压密码。如果指定了该参数,会覆盖sefpassword()中指定的默认解码密码。

ZipFile.getinfo(name)
返回压缩文件中名为name的文件的ZipInfo对象。如果name文件不存在,则抛出KeyError异常

ZipInfo.comment
文件的注释


6. 通过一系列的处理,终于找到了注释,依次打印出来,得到的是由字符组成的hockey的图案。然后很开心的把hockey放到url中。

7. 结果给出这么一串字母。it’s in the air. look at the letters.再次懵逼,看了看前人的答案,才知道是oxygen(氧气),然后oxygen就是组成HOCKEY的小写字母。(特么的,真心想不到)

import re
import zipfile

z = zipfile.ZipFile('channel.zip', 'r')
pattern = re.compile(r'\ ([0-9].*[0-9]*)')
nothing = '90052'
comments = []
while True:
text = z.read(nothing + '.txt')
comments.append(z.getinfo(nothing + '.txt').comment)
print text
rel  = re.search(pattern, text)
if not rel: break
nothing = rel.group(1)
print comments
for x in comments:
print x,


answer: oxygen
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python