Skip to content

S2Q2 · Abbreviate Initials and Sort

⚡ Quick Reference

Type: Full I/O problem

Core idea: split each name, abbreviate all parts except the last, sort, print.

n = int(input())
results = []
for _ in range(n):
    parts = input().split()
    abbreviated = [p[0] + "." for p in parts[:-1]] + [parts[-1]]
    results.append(" ".join(abbreviated))

for name in sorted(results):
    print(name)

Key rules: - All parts except the last → first letter + "." - Last part → kept in full - Sort the abbreviated names alphabetically before printing


Problem Statement

Problem (I/O type)

Read n full names. Abbreviate all name parts except the last (surname), then print the results sorted alphabetically.

Example:

Input
3
John Doe
Alice Johnson
Bob Alan Rickman
Output
A. Johnson
B. A. Rickman
J. Doe

Understanding the transformation

"John Doe"         → parts = ["John", "Doe"]
                   → initials = ["J."]  last = "Doe"
                   → "J. Doe"

"Alice Johnson"    → parts = ["Alice", "Johnson"]
                   → initials = ["A."]  last = "Johnson"
                   → "A. Johnson"

"Bob Alan Rickman" → parts = ["Bob", "Alan", "Rickman"]
                   → initials = ["B.", "A."]  last = "Rickman"
                   → "B. A. Rickman"

After abbreviation: ["J. Doe", "A. Johnson", "B. A. Rickman"]

Sorted alphabetically: ["A. Johnson", "B. A. Rickman", "J. Doe"]

parts[:-1] and parts[-1]

parts[:-1] gives all parts except the last; parts[-1] gives just the last part. This works for names with any number of parts - two words, three words, or more.


Tracing the example

Full name parts Abbreviated Result
"John Doe" ["John","Doe"] ["J."] + ["Doe"] "J. Doe"
"Alice Johnson" ["Alice","Johnson"] ["A."] + ["Johnson"] "A. Johnson"
"Bob Alan Rickman" ["Bob","Alan","Rickman"] ["B.","A."] + ["Rickman"] "B. A. Rickman"

Sorted: A. Johnson, B. A. Rickman, J. Doe


Solution approaches

n = int(input())
results = []
for _ in range(n):
    parts = input().split()
    abbreviated = [p[0] + "." for p in parts[:-1]] + [parts[-1]]
    results.append(" ".join(abbreviated))

for name in sorted(results):
    print(name)
n = int(input())
results = []

for _ in range(n):
    parts = input().split()
    last_name  = parts[-1]
    first_parts = parts[:-1]

    initials = []
    for part in first_parts:
        initials.append(part[0] + ".")

    abbreviated = " ".join(initials + [last_name])
    results.append(abbreviated)

results.sort()
for name in results:
    print(name)
n = int(input())
names = [input().split() for _ in range(n)]

def abbreviate(parts):
    return " ".join([p[0] + "." for p in parts[:-1]] + [parts[-1]])

results = sorted(map(abbreviate, names))
print("\n".join(results))

map(abbreviate, names) applies the function to each name's parts. "\n".join() prints all in one call.

n = int(input())
abbreviate = lambda parts: " ".join([p[0]+"." for p in parts[:-1]] + [parts[-1]])
results = sorted(abbreviate(input().split()) for _ in range(n))
for name in results:
    print(name)

Key takeaways

01

parts[:-1] and parts[-1] split first from last

parts[:-1] gives every part except the surname; parts[-1] gives just the surname. Works for any number of name parts - two, three, or more - without special-casing.

02

p[0] + "." abbreviates each part

The first character of each part followed by a full stop gives the initial. A list comprehension over parts[:-1] handles all middle names in one line.

03

Sort the abbreviated names, not the originals

Sorting happens after abbreviation - the output order is alphabetical by the abbreviated name. Build the full result list first, then pass to sorted().