Brianshun 2023-02-08 10:17 采纳率: 66.7%
浏览 39
已结题

python问题求答案啊!急!

python问题求答案啊!急!
PROBLEM: Given a sentence (up to 1024 characters long), output the following:
#1) The number of different letters. This will be a number from 1 to 26, inclusive.
2) The number of vowels. Vowels are the letters a, e, i, o, and u.
3) The number of uppercase letters.
4) The number of times that the most frequent letter appears. There is no distinction between
lowercase and uppercase letters.
5) The longest word in the sentence. If there is a tie, print the one that appears first when sorting
these words alphabetically without regard to lowercase and uppercase.
INPUT: One line of data, containing a sentence, up to 1024 characters long.
OUTPUT: Print the five statistics specified above in that order.
SAMPLE INPUT
The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.
SAMPLE OUTPUT
#1. 25
2. 19
3. 3
4. 6
5. Roxanne

  • 写回答

4条回答 默认 最新

  • johnny233 Java后端领域新星创作者 2023-02-08 11:04
    关注
    # 获取全部不同的字母个数
    def get_diff_letter(input_str):
        return len(set(list(filter(str.isalpha, input_str))))
    
    
    # 获取元音字母次数
    def get_num_of_vowel(input_str):
        print(sum([1 for letter in input_str if letter.lower() in ['a', 'e', 'i', 'o', 'u']]))
    
    
    # 大写字母开头的单词个数
    def num_of_upper(input_str):
        print(sum([1 for letter in input_str if letter.isupper()]))
    
    
    # 出现次数最多的字母
    def word_of_freq(input_str):
        most_freq = 0
        for letter in input_str:
            num_letter = input_str.count(letter)
            most_freq = max(most_freq, num_letter)
        print(most_freq)
    
    
    # 最长的单词
    def longest(input_str):
        input_str = input_str.replace(',', '').replace('.', '') # 过滤掉英文逗号和句号
        words = input_str.split(' ')
        print(max(words, key=len))
    
    
    if __name__ == '__main__':
        input_str = 'The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.'
        get_diff_letter(input_str)
        get_num_of_vowel(input_str)
        num_of_upper(input_str)
        word_of_freq(input_str)
        longest(input_str)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 创建了问题 2月8日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分