checkio-the most wanted letter

给你一个其中包含不同的英文字母和标点符号的文本,你要找到其中出现最多的字母,返回的字母必须是小写形式,
当检查最想要的字母时,不区分大小写,所以在你的搜索中 “A” == “a”。 请确保你不计算标点符号,数字和空格,只计算字母。
如果你找到 两个或两个以上的具有相同的频率的字母, 返回那个先出现在字母表中的字母。 例如 – “one”包含“o”,“n”,“e”每个字母一次,因此我们选择“e”。
输入: 用于分析的文本 (str, unicode).
输出: 最常见的字母的小写形式。
前提::
密码只包含ASCII码符号
0 < len(text) ≤ 105

import collections
import string
#import re
def 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 solution


if __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]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Beijing, the capital city of China, is a fascinating place with a rich history and culture. It's a city that has something to offer for everyone, from foodies and shoppers to history buffs and nature lovers. However, there is one place in Beijing that stands out as the most interesting and captivating: The Forbidden City. The Forbidden City is a magnificent palace complex located in the heart of Beijing. It was once the home of the imperial family during the Ming and Qing dynasties, and today it's one of the most popular tourist attractions in China. The complex covers an area of 720,000 square meters and consists of more than 800 buildings, making it the largest palace complex in the world. The Forbidden City is surrounded by a moat and a 10-meter-high wall, which adds to its grandeur and mystique. The main entrance to the palace complex is through the Meridian Gate, which is guarded by two lion statues. Once inside, visitors are transported back in time to a world of imperial splendor and opulence. The palace complex is divided into two main sections, the Outer Court and the Inner Court. The Outer Court was used for ceremonial purposes and is where the emperor would receive officials and hold important ceremonies. The Inner Court was the private residence of the imperial family and is where the emperor and his family would live. One of the most impressive buildings in the Forbidden City is the Hall of Supreme Harmony, which is located in the Outer Court. This magnificent hall was used for important ceremonies such as the coronation of emperors and the proclamation of military victories. The hall is supported by 72 wooden columns, each made from a single tree, and the roof is adorned with intricate dragon and phoenix designs. Another must-see attraction in the Forbidden City is the Palace Museum, which is located in the Inner Court. The museum houses a vast collection of imperial treasures, including jade carvings, porcelain, and ancient calligraphy. Visitors can also see the living quarters of the imperial family, including the bedrooms, living rooms, and study rooms. Overall, the Forbidden City is a must-visit destination for anyone traveling to Beijing. It's a place that captures the essence of imperial China and offers a glimpse into the ancient world of the Ming and Qing dynasties. With its grandeur, history, and cultural significance, the Forbidden City is truly the most interesting place in Beijing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值