Today
Total
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
관리 메뉴

기억은 짧고 기록은 길다

[프로그래머스/Programmers] 오픈채팅방 - Python 본문

CodingTest/programmers

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

ukunV 2021. 10. 7. 16:04

Link

 

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

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

programmers.co.kr

 

Solution

def solution(record):
    log = []

    d = {}
    for i in range(len(record)):
        temp = record[i].split(' ')
        if temp[0] == 'Leave':
            log.append([temp[1], "님이 나갔습니다."])
        elif temp[0] == 'Enter':
            d[temp[1]] = temp[2]
            log.append([temp[1], "님이 들어왔습니다."])
        elif temp[0] == 'Change':
            d[temp[1]] = temp[2]

    answer = []

    for i in range(len(log)):
        answer.append(d[log[i][0]] + log[i][1])

    return answer
🔑 key point: Dictionary

 

Other Solution

def solution(record):
    answer = []
    namespace = {}
    printer = {'Enter':'님이 들어왔습니다.', 'Leave':'님이 나갔습니다.'}

    for r in record:
        t = r.split(' ')
        if t[0] in ['Enter', 'Change']:
            namespace[t[1]] = t[2]

    for r in record:
        t = r.split(' ')
        if t[0] != 'Change':
            answer.append(namespace[t[1]] + printer[t[0]])

    return answer
🔑 key point: Dictionary
Comments