checkio-the most wanted letter
来源:互联网 发布:篮球队logo设计软件 编辑:程序博客网 时间:2024/05/29 12:56
给你一个其中包含不同的英文字母和标点符号的文本,你要找到其中出现最多的字母,返回的字母必须是小写形式,
当检查最想要的字母时,不区分大小写,所以在你的搜索中 “A” == “a”。 请确保你不计算标点符号,数字和空格,只计算字母。
如果你找到 两个或两个以上的具有相同的频率的字母, 返回那个先出现在字母表中的字母。 例如 – “one”包含“o”,“n”,“e”每个字母一次,因此我们选择“e”。
输入: 用于分析的文本 (str, unicode).
输出: 最常见的字母的小写形式。
前提::
密码只包含ASCII码符号
0 < len(text) ≤ 105
import collectionsimport string#import redef checkio(text): text1=[] text=text.lower() #将大写转换为小写字母 text=filter(str.isalpha,text) #去掉text字符串中非字母的字符 text1=list(text) #将text转换为list #b=collections.Counter(text1) b=collections.Counter(text1).most_common() #返回的是一个字典,common(k)里面的k是多少就返回一个列表,最常出现的几个字符,k空就返回全部 n_text=[] n_text2=[] x=b[0][1] for i in range(0,len(b)): if b[i][1]==x: n_text.append(b[i][0]) n_text2=list(set(n_text)) #去掉list中重复的元素 n_text2.sort() #将n_text2排序,默认从小到大 return n_text2[0] #replace this for solutionif __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert checkio("Hello World!") == "l", "Hello test" assert checkio("How do you do?") == "o", "O is most wanted" assert checkio("One") == "e", "All letter only once." assert checkio("Oops!") == "o", "Don't forget about lower case." assert checkio("AAaooo!!!!") == "a", "Only letters." assert checkio("abe") == "a", "The First." print("Start the long test") assert checkio("a" * 9000 + "b" * 1000) == "a", "Long." print("The local tests are done.")
基本思想是将字符串转换成列表,按照字符出现次数排列,取出出现次数最多的那一个或几个字符。然后出现次数为max的字符全部append进n_text2列表。
然后在n_text2中去掉重复的字符,sort排序。输出n_text2[0]
0 0
- checkio-the most wanted letter
- CheckIO The Most Wanted Letter
- Python实现The Most Wanted Letter详解
- 出现最多次的字符 The Most Wanted Letter
- checkio the most frequent weekdays
- Python checkio "The Most Frequent Weekdays"解决方案
- Illustrator CS Most Wanted: Techniques and Effects
- checkio (Determine the order)
- 【checkio】the Moore neighbourhood
- checkio-the flat dictionary
- CheckIO The Longest Palindromic
- checkio的一些题:Speech Module,Feed Pigeons,Roman numerals,Golden Pyramid,The Most Numbers,Digits Multipl
- Wanted
- What's the most common 9-letter word in RFC 3280?
- 12 Most-Wanted Sublime Text Tips And Tricks
- checkio--
- The most
- Letter to the Sky
- Android5.X (四) : Material Design-动画效果
- 内联函数总结
- [leetcode] 257. Binary Tree Paths 解题报告
- 山东省第六届蓝桥杯 ///标题:加法变乘法//c/c++组
- Ehcache(03)——Ehcache中储存缓存的方式
- checkio-the most wanted letter
- SQL语句判断数据库、表、字段是否存在
- 安卓学习记录-java-day7
- Java 实现导出excel表 POI
- 【源码剖析】threadpool —— 基于 pthread 实现的简单线程池
- 网页运行js代码
- 2015年热门界面开发组件
- C# 网站部署IIS常见异常及解决方法
- iOS开发 .bundle使用