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

HO94·2021년 7월 18일
0

프로그래머스

목록 보기
7/13

2021.07.18

오픈채팅방

문제

채팅방에 들어오고 나가거나, 닉네임을 변경한 기록이 담긴 문자열 배열 record가 매개변수로 주어질 때, 모든 기록이 처리된 후, 최종적으로 방을 개설한 사람이 보게 되는 메시지를 문자열 배열 형태로 return 하도록 solution 함수를 완성하라.

...중략...

처음 코드

dic_enter = {}
dic_leave = {}
dic_change = {}
for i in record:
    if "Enter" in i:
        enter, enterID, enterName = i.split(" ")
        dic_enter[enterID] = enterName
        print(dic_enter[enterID], "님이 들어왔습니다.")

    if "Leave" in i:
        leave, leaveID = i.split(" ")
        print(dic_enter[leaveID], "님이 나갔습니다.")

    if "Change" in i :
        change, changeID, changeName = i.split(" ")
        dic_enter[changeID] = changeName

오늘도 뭔가 풀릴듯하면서 못풀었다.
같이 스터디하는 친구가 푼 코드와 설명을 들으니 접근은 제대로 했는데
구현을 못한거였다,,알고리즘 공부를 따로 해야겠다는 생각이 강하게 들었다

수정 코드

def solution(record):
    answer = []
    dic_ = {}

    for i in record:
        temp = i.split(" ")
        
        if len(temp) == 3:
            dic_[temp[1]] = temp[2]

    for i in record:
        temp = i.split(" ") 
        
        if temp[0] == "Enter":
            answer.append(dic_[temp[1]] + "님이 들어왔습니다.")
        elif temp[0] == "Leave":
            answer.append(dic_[temp[1]] + "님이 나갔습니다.")
            
    return answer

0개의 댓글