Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

pychallenge(2) - rare characters

$
0
0

pychallenge之二

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


pychallenge(2) - rare characters

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:

-->

<!--

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

...

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

# -*- coding: utf-8 -*- def openfile(file): """ :type file: str :rtype: dict """ dic = {} with open(file) as f: for line in f.readlines(): for ch in line: if ch not in dic.keys() and ch != '\n': dic[ch] = 1 elif ch != '\n': dic[ch] += 1 return dic if __name__ == '__main__': res = openfile('C:\Users\Katsu\Desktop\\rare.txt') # rare.txt存放网页里的乱码 print res print res.keys() print res.values() print sorted(res.values()) 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。


Viewing all articles
Browse latest Browse all 9596

Trending Articles