Python/Programmers

[프로그래머스/Python] Lv 2. 오픈채팅방

hwangzzi 2023. 4. 17. 13:10

⭐ 문제 링크

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

⭐ 풀이 코드

 

런타임 에러

# 테스트케이스 통과, 런타임 에러 

def solution(record):
    answer = []
    
    command = []
    members = {}
    
    for r in record:
        temp = list(r.split())
        if temp[0] == "Leave":
            command.append((temp[0], temp[1]))
            del members[temp[1]]
        else:
            command.append((temp[0], temp[1]))
            members[temp[1]] = temp[2]
        
    for c in command:
        if c[0] == "Enter":
            answer.append(members[c[1]] + "님이 들어왔습니다.")
        if c[0] == "Leave":
            answer.append(members[c[1]] + "님이 나갔습니다.")
    #print(command)
                
    
    
    return answer

 

 통과

def solution(record):
    answer = []
    
    command = [list(r.split()) for r in record]  # record 각각 list에 넣기
    members = {}  # 아이디 : 이름
    
    for c in command:
        if len(c) == 3:  # 명령이 Enter 또는 Change일 경우 
            members[c[1]] = c[2]
            
    for c in command:
        if c[0] == "Enter":
            answer.append(members[c[1]] + "님이 들어왔습니다.")
        if c[0] == "Leave":
            answer.append(members[c[1]] + "님이 나갔습니다.")        
    
    return answer