동캄의 코딩도장

프로그래머스 level2 [오픈채팅방] 파이썬 본문

코테/프로그래머스

프로그래머스 level2 [오픈채팅방] 파이썬

동 캄 2021. 12. 10. 11:14

https://programmers.co.kr/learn/courses/30/lessons/42888

 

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

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

programmers.co.kr

#프로그래머스 오픈채팅방
def solution(record):
    answer = []
    lst={}
    prompt=[]
    for line in record:
        s=list(map(str,line.split()))
        if s[0]!='Leave':
            lst[s[1]]=s[2]
        prompt.append(s)
    for line in prompt:
        if line[0]=='Enter':
            answer.append("{}님이 들어왔습니다.".format(lst[line[1]]))
        elif line[0]=='Leave':
            answer.append("{}님이 나갔습니다.".format(lst[line[1]]))
    return answer

앞의 for문을 통해 최종 아이디와 이름을 매칭시킨다.

다음 for문을 통해 메세지를 생성한다.