Normalize Names
Python
Medium
2 views
Problem Description
Read n names (one per line). Make function norm(name) which trims spaces and converts to Title Case (first letter uppercase, rest lowercase per word). Output normalized names.
Input Format
First line n. Next n lines name (may contain spaces).
Output Format
n lines normalized.
Sample Test Case
Input:
3
ravi kumar
NEHA shARMA
aMAN
Output:
Ravi Kumar
Neha Sharma
Aman
Constraints
Total characters
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
def norm(s):
s=s.strip()
parts=[p for p in s.split(' ') if p!='']
out=[]
for w in parts:
if w=='':
continue
out.append(w[:1].upper()+w[1:].lower() if len(w)>0 else '')
return ' '.join(out)
out=[]
for i in range(1,1+n):
out.append(norm(lines[i] if i<len(lines) else ''))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!