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

时间: 2024-11-12 19:31:44

The Most Wanted Letter的相关文章

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

[USACO 2011 Dec Gold] Threatening Letter【后缀】

Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and wants to send him a nasty letter, but wants to remain anonymous. As so many before him have done, he plans to cut out printed letters and paste them on

Letter Combinations of a Phone Number

1. Question 给一个数字字符串,按照电话上各个数字对应的字母,返回该字符串代表的所有可能的字母组合. Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit stri

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outpu

a letter and a number

 a letter and a number描述 we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26; Give you a letter x and a number y , you should output the result of y+f(x). 输入 On the first line, contains a number T(0<T<=10000).then T lines

leetcode第18题--Letter Combinations of a Phone Number

Problem: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae&

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

Leetcode - Letter Combination Of A Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

【LeetCode】17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode17 Letter Combinations of a Phone Number

题意: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"