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的更多相关文章

  1. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  3. 什么是Unicode letter

    起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  4. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

  6. SCI/EI期刊投稿 Reply Letter 常用格式总结

    SCI/EI期刊投稿Reply Letter常用格式总结          整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...

  7. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. [LeetCode] Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  10. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

随机推荐

  1. bzoj1143

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1143 首先用传递闭包,知道一个点是否可以到达另一个点,即mp[i][j]==1表示i可以到j: ...

  2. 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应

    http://blog.csdn.net/panjunbiao/article/details/8615880   用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应

  3. 【POJ2777】Count Color(线段树)

    以下是题目大意: 有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:C X Y Z —— 从X到Y将板子涂成颜色ZP X Y    —— 查询X到Y的板子共 ...

  4. bootstrap学习以及其插件

    Bootstrap中文网地址,里面有bootstrap组件的下载与使用说明,现在使用bootstrap3: http://www.bootcss.com/ W3CSchool.CC里面有学习boots ...

  5. ffmpeg + sdl -03 简单音频播放器实现

    没办法,工作中遇到了问题. 目前NEC EMMA的架构如下: 从USB读入文件 -> 文件分析并提取Packet中的Payload Data   -> NEC HANDLE AVTrans ...

  6. String源码学习

    String源码学习 零散的收获 数组的元素类型可以通过getComponentType来获取到 子类型的数组可以赋值给父类型的数组,.但是并不存在继承关系.数组的父类是Object. 通过声明如下代 ...

  7. docke 网络配置

    一,docker 的bridge模式是和vmware中的nat模式类似的,但是如果想要弄成和vmwae中的bridge怎么办呢? 说明,bridge模式获取的Ip是与宿主机的ip是出于同一个网段的. ...

  8. android-用xml自定义背景(可自定义显示具体那一边)

    常见的描边都是闭合的.四个边都有.如下: <?xml version="1.0" encoding="UTF-8"?> <layer-list ...

  9. 使用Fiddler抓取手机上的数据包

    在IIS中,如果网站已经绑定了域名在使用IP是不能访问的,需要添加一个空的主机名与IP的映射才能访问.如下图: Fiddler抓取手机包 在PC上建一个WIFI热的 勾选Fiddler中Tool-&g ...

  10. 前台传来的文件通过流stream转成bytes 再把文件写入数据库 类型是blob

    //获取前台传来的文件 HttpFileCollection files = HttpContext.Current.Request.Files; Stream st = files[0].Input ...