Brianshun 2023-02-08 10:29 采纳率: 66.7%
浏览 34

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
已关注,能帮我看看吗?

  • 写回答

2条回答 默认 最新

  • 程序员星辰 2023-02-08 11:10
    关注

    以下为代码,有用点采纳

    
    sentence = input().strip()
    letters = {}
    vowels = 0
    uppercase = 0
    longest_word = ""
    
    for char in sentence:
        if char.isalpha():
            if char.lower() in letters:
                letters[char.lower()] += 1
            else:
                letters[char.lower()] = 1
        if char.lower() in "aeiou":
            vowels += 1
        if char.isupper():
            uppercase += 1
    
    words = sentence.split()
    for word in words:
        if len(word) > len(longest_word):
            longest_word = word
        elif len(word) == len(longest_word) and word < longest_word:
            longest_word = word
    
    print("#1.", len(letters))
    print("2. ", vowels)
    print("3. ", uppercase)
    print("4. ", max(letters.values()))
    print("5. ", longest_word)
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 2月8日