yzc164 发表于 2017-4-23 14:24:23

python challenge 2

根据网页上的提示,打开网页源代码,有一句:find rare characters in the mess below:
意思是找出出现次数非常少的字母。于是统计这段mess中的每个字符出现的次数。
其实像%$@_这些都可以不用统计了,应该提示的是找出characters。
查出aeilquty这几个字母出现的次数是1,组合出来的单词有equality,这个词就是答案了。

import re
if __name__ == '__main__':
# put the mess from the page source into 2.txt
f = open('2.txt', 'r')
text = f.read()
#solution 1 Start
dic = {}
for x in text:
if x in dic:
dic += 1
else:
dic = 1
print('solution 1 :')
for x in dic:
print '%s, %d' % (x, dic)   
#solution 1 End
#solution 2 Start
dict = {}
for x in text:
dict = dict.get(x, 0) + 1;
print('solution 2:')
print(''.join( < 10]))
#solution 2 End
#solution 3 Start
print('solution 3:')
print ''.join()
#solution 3 End
#solution 4 Start
print('solution 4:')
print(re.findall('', text))
#solution 4 End
f.close();

学习了re模块的使用。
页: [1]
查看完整版本: python challenge 2