단순 구현, 조금 더 고차원 list를 다루는 데에 익숙했으면 쉽게 금방 풀이를 완료했을 수 있었을 것이다.
"""
https://www.acmicpc.net/problem/1283
"""
N = int(input())
keys = [] # 대문자로 통일
words = [ input() for _ in range(N) ]
for word in words:
check = 0
l = word.split()
for j in l: # 단어 첫글자 기준 key값 추가
if j[0].upper() not in keys:
keys.append(j[0].upper())
check += 1
break
if check == 0: # 단어 첫글자가 모두 key로 존재한다면
l = ''.join(l)
for k in list(l):
if k.upper() not in keys:
keys.append(k.upper())
check += 1
break
if check == 0:
keys.append('.')
ans = []
for key,word in zip(keys,words):
check = 0
l = word.split()
word = list(word)
for idx,j in enumerate(l):
if j[0].upper() == key:
new = list(j)
new[0] = '['+new[0]+']'
check += 1
l[idx] = ''.join(new)
ans.append(l)
break
if check == 0:
for idx,i in enumerate(word):
if i == ' ':
pass
if i.upper() == key:
word[idx] = '['+i+']'
check += 1
word = ''.join(word)
ans.append([word])
break
if check == 0:
ans.append([''.join(word)])
for i in ans:
print(' '.join(i))
n = int(input())
words = []
for _ in range(n):
words.append(input())
shortcut = set([' ']);
splited = []
for w in words:
temp = w.split()
haveShortcut = False
for i in range(len(temp)):
if temp[i][0].upper() not in shortcut:
shortcut.add(temp[i][0].upper())
temp[i] = '[' + temp[i][0] + ']' + temp[i][1:]
splited.append(" ".join(temp))
haveShortcut = True
break
if not haveShortcut:
for i in range(len(w)):
if w[i].upper() not in shortcut:
shortcut.add(w[i].upper())
splited.append(w[:i] + '[' + w[i] + ']' + w[i+1:])
haveShortcut = True
break
if not haveShortcut:
splited.append(" ".join(temp))
for s in splited:
print(s)
여기서 나는 word에 대해 list를 만들고 각 원소에 대해 다시 list를 만들어서 두 개의 for문으로 접근했다면 word[a][b]형태로 한 번에 접근 가능하도록 하는 것이 코드를 간편하게 만드는 데에 있어서 도움이 될 것이다.