easy +10 pts

Abbreviate Name

Transform a full name into an abbreviated form with initials and surname.

Write a function `abbreviate_name(name: str) -> str` that takes a full name as a string containing one or more words separated by single spaces. The function should return a string where all words except the last one are reduced to their first uppercase letter followed by a period and a space, and the last word is capitalized. - Preserve the original order of words. - Each initial should be the uppercase first character of the corresponding word. - The last word should be returned with its first character uppercase and the rest of the word lowercase (if it is already fully uppercase, convert it to title case). - If the input has only one word, return that word in title case (no initials). - The input will not be empty and will not contain leading/trailing spaces. Examples: - `abbreviate_name("John Doe")` → `"J. Doe"` - `abbreviate_name("ALICE Smith")` → `"A. Smith"` - `abbreviate_name("Robert James Miller")` → `"R. J. Miller"` - `abbreviate_name("Madonna")` → `"Madonna"` Implement the function exactly as specified.

Constraints

- Input string length is between 1 and 100 characters. - The name consists of uppercase/lowercase letters and spaces only. - Words are separated by exactly one space. - No leading or trailing spaces. - Time complexity: O(n), where n is the length of the input string.

Example

>>> abbreviate_name("John Doe")
'J. Doe'
>>> abbreviate_name("ALICE Smith")
'A. Smith'
>>> abbreviate_name("Robert James Miller")
'R. J. Miller'
>>> abbreviate_name("Madonna")
'Madonna'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `split()` to separate the name into words.
Take the first character of each word except the last and uppercase it.
For the last word, use `.capitalize()` to convert to title case.
Join the initials with a period and space, then append the last name.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.