pythonchallenge是一個頗有意思的學習python的網站,經過用程序解開一個謎,能夠進入到下一個level,總共有幾十個level,網址是http://www.pythonchallenge.com。很早就知道這個網站了,一直放在書籤裏沒動,最近下定決心要學習python了,心血來潮決定玩玩這個網站。level0和level1都挺簡單的(其實level2也挺簡單的),就從level2開始記錄個人歷程吧。html
個人python代碼以下:python
import urllib2 content=urllib2.urlopen('http://www.pythonchallenge.com/pc/def/ocr.html').read() content=content[content.find('<!--',800)+4:len(content)-5] dict1={} for c in content: if c in dict1: dict1[c]+=1 else: dict1[c]=1; dict2= sorted(dict1.iteritems(), key=lambda d:d[1], reverse = True) print dict2
思路就是,題目其實藏在網頁源碼裏,是一串特別特別長的字符串,從中找出幾個低頻的字符,我懶得copy那段字符了,直接用python庫利用url取得html代碼,從中截取出那段字符,再使用一個dict來記錄每一個字符出現的次數,最後將dict排序,最後的輸出是學習
[(')', 6186), ('@', 6157), ('(', 6154), (']', 6152), ('#', 6115), ('_', 6112), ('[', 6108), ('}', 6105), ('%', 6104), ('!', 6079), ('+', 6066), ('$', 6046), ('{', 6046), ('&', 6043), ('*', 6034), ('^', 6030), ('\n', 1221), ('a', 1), ('e', 1), ('i', 1), ('l', 1), ('q', 1), ('u', 1), ('t', 1), ('y', 1)]
頻率最低的幾個詞組成的單詞是equality,因此level2的解題url是http://www.pythonchallenge.com/pc/def/equality.html。網站