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. 算法导论(第三版) Exercises4.2(求最大和子数组的算法优化过程)

    4.1-1 如所有元素都为负,则返回所有元素中最大的负数. 4.1-2(暴力法求最大和子数组) struct subarray { int start, end, sum; }; void brute ...

  2. MyBatis映射文件的resultMap如何做表关联

    MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射 ...

  3. [Matlab] Attempt to execute SCRIPT *** as a function

    Attempt to execute SCRIPT *** as a function 问题: 在运行MATLAB程序的时候,出现如题的报错. 原因: 在系统中,现有的.m文件有的与***函数重名,所 ...

  4. TTB 基本

    中文名 ,线程构建模块 外文名 Thread Building Blocks 缩    写 TBB 开    发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承     1 ...

  5. js jsp 时间 日期 控件 插件 简单 实用

    js时间控件一般都是找网上的用,这东西平常很少涉及到,一用到找起来却烦死人,不是没用就是太复杂,今天向大家推荐一个简单实用的控件,该控件在不断更新,而且有专门的网站对它进行维护,所以值得一看. 先说它 ...

  6. (转) .NET实现Repeater控件+AspNetPager控件分页

    SqlConnection (.NET C#) 连接及分页 .net的访问数据机制决定了访问大量数据时会致使客户端机器消耗大量资源,因此有必要对数据进行分页显示,开发工具vs.net+sqlserve ...

  7. Android与JS混编(js调用android相机)

       参考android相机调用,http://blog.csdn.net/yanzi1225627/article/details/33028041/,谢谢 相机怎么调用就不做赘述了,下面是js调用 ...

  8. c#类似单片机的8bit或运算

    1.正确 PWMSUBM0 &= (byte)(PWMSUBM0 | 0xfc); PWMSUBM0 &= (byte)(PWMSUBM0 | (byte)0xfc); 2.不能编译的 ...

  9. 美化xterm

    很多软件调试时,会打开xterm,不过很难看,字体.背景等等都不好看,网上找到了一个不错的xterm的配置文件 !look and feel xterm.termName: xterm-256colo ...

  10. linux系统结构和系统命令初步

    以上是第五课和第14课笔记 linux 基本结构: 系统构成:kernel,Modules,Lib,(Shell,Tool)系统引导:BIOS -> Bootlooder -> Kerne ...