pychallenge(2) - rare characters

2023-11-27 01:20
文章标签 characters rare pychallenge

本文主要是介绍pychallenge(2) - 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。

 

 

 

 

 

转载于:https://www.cnblogs.com/katsu/p/6516753.html

这篇关于pychallenge(2) - rare characters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/426696

相关文章

解决The valid characters are defined in RFC 7230 and RFC 3986

解决方法: 一、更换低版本的Tomcat;(我选的方案) 二、参考:https://blog.csdn.net/qq_32365919/article/details/82055800

【python 编码问题】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not

插入oracle 数据发生 错误:UnicodeEncodeError: 'ascii' codec can't encode characters in position 131-136: ordinal not in range(128) 先说解决办法: python2.7版本,在开头加入下面语句 import sysreload(sys)sys.setdefaultencoding

HYPERCASUAL - Simple Characters(卡通游戏火柴人物模型)

介绍HyperCasual - 简单角色! 一套低多边形角色资源,用于创建超休闲风格的游戏。 包含演示场景 角色(x10) 生化人、小丑、Flaty_Boss、女孩、守门员、英雄、亚马逊女战士、男人、红衣男人、修理工 每个网格大约有700-2000个顶点 角色设置与Mecanim兼容(本包中不包含动画) 着色器适用于可编写脚本的渲染管线(HD + LW) 下载:​​Unity资源商店链接资源

Longest Substring with At Most K Distinct Characters

Given a string, find the length of the longest substring T that contains at mostk distinct characters. For example,Given s = “eceba” and k = 2, T is "ece" which its length is 3. 思路:跟  Longest Sub

Longest Substring with At Most Two Distinct Characters

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example,Given s = “eceba”, T is "ece" which its length is 3. 思路:同向双指针,跟Longest Substrin

Replace All ?‘s to Avoid Consecutive Repeating Characters

Given a string s containing only lower case English letters and the '?' character, convert all the '?' characters into lower case letters such that the final string does not contain any consecutive re

Swift 3.0 学习 -- 计算字符数量 (Counting Characters)

在swift2.0的时候,可以通过调用全局countElements函数,并将字符串作为参数进行传递,可以获取该字符串的字符数量。如下: let unusualMenagerie = "Koala, Snail, Penguin, Dromedary"print("unusualMenagerie has \(countElements(unusualMenagerie)) charact

LeetCode #3. Longest Substring Without Repeating Characters

题意: 计算一个字符串的中的最长的不含有重复字母的长度 解法: 尺取法的裸题了,维护2个指针l,r, 不断移动r指针,同时检查[l,r]是不是存在重复的了,如果存在就移动l指针了 class Solution {public:int lengthOfLongestSubstring(string s) {int n = s.size();int l=0, r=0;set<char> S;in

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

问题背景 在python2中安装了labelme,可以正常运行,然后又再python3中安装了labelme。后来python2中的labelme不能运行,python3中的labelme可以运行。 具体问题 UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-3: ordinal not in ra