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。