pythonchallenge 2

   pythonchallenge是一个很有意思的学习python的网站,通过用程序解开一个谜,可以进入到下一个level,总共有几十个level,网址是http://www.pythonchallenge.com。很早就知道这个网站了,一直放在书签里没动,最近下定决心要学习python了,心血来潮决定玩玩这个网站。level0和level1都挺简单的(其实level2也挺简单的),就从level2开始记录我的历程吧。

  我的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), ('
', 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

原文地址:https://www.cnblogs.com/ValiancyHe/p/3425517.html