알고리즘/프로그래머스

[Python] 프로그래머스 오픈채팅방

컴공맨 2021. 4. 11. 22:36
 

코딩테스트 연습 - 오픈채팅방

오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오

programmers.co.kr


풀이

딕셔너리를 사용해서 사용자들의 최종 닉네임을 기록하고 그 다음으로 명령어를 처리하면서 uid를 사용하여 최종닉네임을 출력하여 해결했습니다.


코드

def solution(record):
    answer = []

    uid_logs = dict()

    # 아이디의 닉네임과 명령어를 기록
    # 이때, change는 출력할 필요 X
    for info in record:
        log = info.split(" ")

        command = log[0]
        uid = log[1]

        if command != "Leave":
            nickname = log[2]
            uid_logs[uid] = nickname

    # 처리한 명령어와 최종 닉네임으로 결과저장
    for info in record:
        log = info.split(" ")

        command = log[0]
        uid = log[1]

        if command == "Enter":
            answer.append(uid_logs[uid] + "님이 들어왔습니다.")
        elif command == "Leave":
            answer.append(uid_logs[uid] + "님이 나갔습니다.")

    return answer

 

pyo7410/Algorithm

1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.

github.com