Scribbling

프로그래머스: 오픈채팅방 본문

Computer Science/Coding Test

프로그래머스: 오픈채팅방

focalpoint 2021. 11. 8. 22:02

2019 카카오 코딩 테스트 기출 문제라고 한다.

단순 구현문제다.

from collections import defaultdict

def solution(record):
    results = []
    record_dict = defaultdict(list)
    nickname_dict = {}
    for rec in record:
        rec = rec.split(' ')
        if rec[0] == 'Enter':
            user_id, nickname = rec[1], rec[2]
            results.append(nickname + "님이 들어왔습니다.")
            record_dict[user_id].append(len(results) - 1)
            if user_id not in nickname_dict:
                nickname_dict[user_id] = nickname
            else:
                if nickname_dict[user_id] != nickname:
                    nickname_dict[user_id] = nickname
                    for idx in record_dict[user_id]:
                        results[idx] = nickname + "님" + results[idx].split('님')[1]
        elif rec[0] == 'Leave':
            user_id = rec[1]
            nickname = nickname_dict[user_id]
            results.append(nickname + "님이 나갔습니다.")
            record_dict[user_id].append(len(results) - 1)
        else:
            user_id, nickname = rec[1], rec[2]
            nickname_dict[user_id] = nickname
            for idx in record_dict[user_id]:
                results[idx] = nickname + "님" + results[idx].split('님')[1]
    return results