목차

오픈채팅방

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호42888
문제명오픈채팅방
레벨Level 2
분류

기초

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python
해결날짜2021/12/31

풀이

코드

"""Solution code for "Programmers 42888. 오픈채팅방".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/42888
- Solution link: http://www.teferi.net/ps/problems/programmers/42888
"""


def solution(record):
    nick_by_id = {}
    for rec in record:
        action, *words = rec.split()
        if action in ('Enter', 'Change'):
            user_id, nickname = words
            nick_by_id[user_id] = nickname

    answer = []
    for rec in record:
        action, user_id, *_ = rec.split()
        if action == 'Enter':
            answer.append(f'{nick_by_id[user_id]}님이 들어왔습니다.')
        elif action == 'Leave':
            answer.append(f'{nick_by_id[user_id]}님이 나갔습니다.')

    return answer