Python Challenge闯关游戏——持续更新

来源:互联网 发布:什么软件看美剧最全 编辑:程序博客网 时间:2024/06/06 09:39

Python Challenge是一个网页版在线闯关游戏,该系列一共有33个项目,每一关都需编写程序寻找答案以通关。本文基于Python2.7,利用PyCharm工具记录了典型通关题目的解决方案,持续更新中…

Level1: 位移加密

//ch1_map.py, ocrimport stringtext = "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 my_solution(test):#     o = ""#     for each in text:#         if ord(each) >= ord('a') and ord(each) <= ord('z'):#             o += chr((ord(each) + 2 - ord('a')) % 26 + ord('a'))#         else:#             o += each#     print odef std_solution(test):    table = string.maketrans(        string.ascii_lowercase,        string.ascii_lowercase[2:] + string.ascii_lowercase[:2]    )    print string.translate(test,table)if __name__ == '__main__':     std_solution(text)     std_solution("map")

Level2: 字符识别

//方案一 ch2_ocr.py, equalityimport string# text = open('ch3_mess.txt').read()## def my_solution(test):##     s=filter(lambda x:x in string.letters,test)#     print ss = ''.join([line.rstrip() for line in open('ch3_mess.txt')])occ={}for c in s:    occ[c] = occ.get(c, 0) + 1avgOC = len(s) // len(occ)if __name__ == '__main__':    print ''.join([c for c in s if occ[c] < avgOC])
//方案二 Using Beautiful Soup>>> import urllib>>> import BeautifulSoup as bs>>> churl = "http://www.pythonchallenge.com/pc/def/ocr.html">>> full = urllib.urlopen(churl).read()>>> src = bs.BeautifulSoup(full)>>> comments = src.findAll(text=lambda text:isinstance(text, bs.Comment))>>> clue = comments[1]>>> for l in clue:...          c = clue.count(l)...          if c < 5: print l, "(", c, ")", ... e ( 1 ) q ( 1 ) u ( 1 ) a ( 1 ) l ( 1 ) i ( 1 ) t ( 1 ) y ( 1 )