문제 링크 : programmers.co.kr/learn/courses/30/lessons/42888?language=python3
코딩테스트 연습 - 오픈채팅방
오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오
programmers.co.kr
도중에 닉네임을 변경하는 경우가 생길 수 있기 때문에, 주어진 records의 순서대로 result를 바로 만들 수는 없습니다.
모든 유저를 [유저 아이디]로 구분한다는 점에 착안해서
[유저 아이디]를 key로 가지고 [닉네임]을 value로 갖는 Dictionary를 이용하여 문제를 해결할 수 있습니다.
먼저 records를 앞에서부터 순차적으로 탐색하며 [유저 아이디]와 [닉네임]을 Dictionary에 저장합니다.
records를 전부 탐색한 뒤에 다시 records를 탐색합니다.
이번에는 첫 단어가 Enter, Leave인 경우 [유저 아이디]를 Dictionary에서 탐색하여 [닉네임]을 찾습니다.
def solution(record):
dic = dict()
for s in record:
cmd, user, name = '','',''
temp = list(s.split())
cmd, user = temp[0], temp[1]
if len(temp) > 2:
name = temp[2]
if cmd == 'Enter':
if not dic.__contains__(user):
dic.update({user:name})
else:
dic[user] = name
elif cmd == 'Change':
dic[user] = name
answer = []
for s in record:
cmd, user, name = '','',''
temp = list(s.split())
cmd, user = temp[0], temp[1]
if len(temp) > 2:
name = temp[2]
sentence = dic[user]
if cmd == 'Enter':
sentence += '님이 들어왔습니다.'
answer.append(sentence)
elif cmd == 'Leave':
sentence += '님이 나갔습니다.'
answer.append(sentence)
return answer'Problems > Programmers' 카테고리의 다른 글
| 보석 쇼핑 (카카오 인턴 2020 기출) (0) | 2021.10.02 |
|---|---|
| 길 찾기 게임 (0) | 2021.04.30 |
| 외벽 점검 (0) | 2021.04.23 |
| 가사 검색 (0) | 2021.04.23 |
| 괄호 변환 (0) | 2021.04.23 |