프로그래머스-오픈채팅방

태태·2023년 5월 23일
0

문제


풀이

파이썬에서 유일한 값을 가지는 딕셔너리로 문제를 풀었다
우선 명령이 'Leave'가 아니라면 딕셔너리에 id,user를 쭉 추가해준후
다시 역순으로 배열을 탐색하여 print를 해준다

샘플데이터1)

test=[
    "Enter uid1234 Muzi", 
    "Enter uid4567 Prodo", 
    "Leave uid1234", 
    "Enter uid1234 Prodo", 
    "Change uid4567 Ryan",
    "Enter uid0000 Taetae",
    "Enter uid1111 WangWang",
    "Change uid1111 GilDong",
    "Leave uid1111",
    "Leave uid0000",
    "Enter uid0000 KaKao"]
    
    print(solution(test))

소스코드

def solution(record):
    answer = []
    dictionary={}
    
    for name in record:
        if name.split()[0] != 'Leave':
            dictionary[name.split()[1]]=name.split()[2]
    
    for name in reversed(record):
        message =   name.split()[0]
        user    =   name.split()[1]
        if message=='Enter':
            answer.insert(0,"{0}님이 들어왔습니다.".format(dictionary[user]))
        elif message == 'Leave':
            answer.insert(0,"{0}님이 나갔습니다.".format(dictionary[user]))
    
    return answer
profile
과정에서 재미를 느끼지 않는데 성공하는 일은 거의 없다

0개의 댓글