设为首页 加入收藏

TOP

pychallenge(2) - rare characters
2017-10-09 17:31:37 】 浏览:5429
Tags:pychallenge rare characters

pychallenge之二

题目是下面一张图片配上一段话

recognize the characters. maybe they are in the book,

but MAYBE they are in the page source.

 

根据提示可以看出网页source里有蹊跷,果不其然,有如下一段文字:

 

</body>
</html>

<!--
find rare characters in the mess below:
-->

<!--
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
@##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[**$&^{$!@#$%)!@(&

...

 

既然是找出稀有字符可以考虑用字典存放字符和字符的出现频率,代码如下:

 

 1 # -*- coding: utf-8 -*-
 2 
 3 def openfile(file):
 4     """
 5     :type file: str
 6     :rtype: dict
 7     """
 8     dic = {}
 9     with open(file) as f:
10         for line in f.readlines():
11             for ch in line:
12                 if ch not in dic.keys() and ch != '\n':
13                     dic[ch] = 1
14                 elif ch != '\n':
15                     dic[ch] += 1
16 
17     return dic
18 
19 if __name__ == '__main__':
20     res = openfile('C:\Users\Katsu\Desktop\\rare.txt')  # rare.txt存放网页里的乱码
21     print res
22     print res.keys()
23     print res.values()
24     print sorted(res.values())
25     print [k for k, v in res.items() if v == 1]
 
 

pycharm里跑出的答案是:

C:\Python27\python.exe D:/Py/test/test.py
{'!': 6079, '#': 6115, '%': 6104, '$': 6046, '&': 6043, ')': 6186, '(': 6154, '+': 6066, '*': 6034, '@': 6157, '[': 6108, ']': 6152, '_': 6112, '^': 6030, 'a': 1, 'e': 1, 'i': 1, 'l': 1, 'q': 1, 'u': 1, 't': 1, 'y': 1, '{': 6046, '}': 6105}
['!', '#', '%', '$', '&', ')', '(', '+', '*', '@', '[', ']', '_', '^', 'a', 'e', 'i', 'l', 'q', 'u', 't', 'y', '{', '}']
[6079, 6115, 6104, 6046, 6043, 6186, 6154, 6066, 6034, 6157, 6108, 6152, 6112, 6030, 1, 1, 1, 1, 1, 1, 1, 1, 6046, 6105]
[1, 1, 1, 1, 1, 1, 1, 1, 6030, 6034, 6043, 6046, 6046, 6066, 6079, 6104, 6105, 6108, 6112, 6115, 6152, 6154, 6157, 6186]
['a', 'e', 'i', 'l', 'q', 'u', 't', 'y'] 

最后一行就是答案了,组合一下应该是equality。

 

 

 

 

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Robot Framework中经常用的第三方.. 下一篇如何利用Python绘制学术论文图表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目