The Most Wanted Letter
The Most Wanted Letter
You are given a text, which contains different english letters and punctuation symbols. You should find the most frequent letter in the text. The letter returned must be in lower case.
While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a". Make sure you do not count punctuation symbols, digits and whitespaces, only letters.
If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet. For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".
Input: A text for analysis as a string (unicode for py2.7).
Output: The most frequent letter in lower case as a string.
题目大义:找出出现次数最多的字母,不区分大小写,如果出现次数相同,则取字典序最小的输出
一开始想需要诸如'a' : 0,即字母到数字的对应,于是想到dict;查阅str手册后,发现str.lower方法,将字符串转换为小写;最后是对dict的排序,需要先按字母排序,再按出现次序排序(ps:python中的排序是稳定的)
- 1 def checkio(text):
- 2 lower_text = text.lower()
- 3
- 4 appear_time = {}
- 5
- 6 for each in lower_text:
- 7 if each.islower():
- 8 if each not in appear_time:
- 9 appear_time[each] = 0
- 10 else:
- 11 appear_time[each] += 1
- 12
- 13
- 14 array = appear_time.items();
- 15 array.sort(key=lambda x:x[0])
- 16 array.sort(key=lambda x:x[1], reverse=True)
- 17
- 18 return array[0][0]
不过呢,这是笨方法,没有充分利用Python的丰富资源,给出大神bryukh的解答
- 1 import string
- 2
- 3 def checkio(text):
- 4 """
- 5 We iterate through latyn alphabet and count each letter in the text.
- 6 Then 'max' selects the most frequent letter.
- 7 For the case when we have several equal letter,
- 8 'max' selects the first from they.
- 9 """
- 10 text = text.lower()
- 11 return max(string.ascii_lowercase, key=text.count)
学艺不精,现在想来key的意思是每个元素的顺序由key决定吧,max的第一参数是ascii码的小写字母,相当于把26个字母算了个遍
The Most Wanted Letter的更多相关文章
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 什么是Unicode letter
起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- No.017:Letter Combinations of a Phone Number
问题: Given a digit string, return all possible letter combinations that the number could represent.A ...
- SCI/EI期刊投稿 Reply Letter 常用格式总结
SCI/EI期刊投稿Reply Letter常用格式总结 整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
随机推荐
- coalesce和nvl函数
coalesce 函数 : Oracle COALESCE函数语法为COALESCE(表达式1,表达式2,...,表达式n),n>=2,此表达式的功能为返回第一个不为空的表达式,如果 都为空则返 ...
- pmp论坛
PMP论坛: http://www.px101.com/specialpmp/ http://www.pmp.cn/ http://www.pmptuan.com/ http://www.mypm.n ...
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...
- HDU5584 LCM Walk 数论
LCM Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- security
- day57:00:26:34
今天开始用博客记录倒计时,也只是为了看看今天做了什么.这也是我第一用博客园记录考研生活了 倒计时57天,我在想每天花时间在这记录生活会不会浪费复习的时间,其实不会的了,不去看微博,少刷新闻....仔细 ...
- python linecache标准库基础学习
#python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...
- 大话分页(补充)——Threadlocal封装offSet和pageSize简化分页工具类
经过前两篇文章(大话分页一.大话分页二)的介绍,我认为我想介绍的东西已经介绍完了,不过想精益求精的童鞋可以继续看本篇文章. 在第一篇文章中介绍了一个分页的工具类(具体请看大话分页一),从实现功能上来说 ...
- [RxJS] Basic DOM Rendering with Subscribe
While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...
- android 4.0之前版本号出现JSONException异常
今天在调试解析server传过来的JSON数据时,在2.3.7的手机上报了以下这样一个异常. 08-07 22:00:29.597: W/System.err(7610): org.json.JSON ...