[프로그래머스 / Python] 오픈채팅방
- 카테고리 없음
- 2021. 7. 3.
https://programmers.co.kr/learn/courses/30/lessons/42888
코딩테스트 연습 - 오픈채팅방
오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오
programmers.co.kr
채팅 기록을 멤버 uid에 맞게 변경해야한다. 명령어가 들어올때마다 매번 log들을 바꿔주면 비효율적이므로 멤버 딕셔너리를 생성후 이름 변경될때마다 따로 update를 해주고 log 메시지는 변경사항을 고려하지않고 uid만을 저장해둔다.
마지막에 모든 변경사항을 log기록과 함께 합쳐주면 된다.
내 코드
def solution(record):
answer = []
logList = []
member = {}
for idx, val in enumerate(record):
record[idx] = val.split(" ")
for idx, val in enumerate(record):
if len(val) == 3:
command, uid, name = val
else:
command, uid = val
if command == 'Enter':
member[uid] = name
logList.append([uid, "님이 들어왔습니다."])
elif command == 'Leave':
logList.append([uid, "님이 나갔습니다."])
else:
member[uid] = name
for log in logList:
uid, message = log
answer.append(member[uid] + message)
return answer