Last active
April 4, 2019 02:53
-
-
Save rpf5573/b784a0f863fb0b25a15f88732c975535 to your computer and use it in GitHub Desktop.
카카오 오픈채팅방 알고리즘 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# from : https://programmers.co.kr/learn/courses/30/lessons/42888?language=python3 | |
ENTER = 'Enter' | |
ENTER_KOREAN = '들어왔습니다' | |
LEAVE = 'Leave' | |
LEAVE_KOREAN = '나갔습니다' | |
CHANGE = 'Change' | |
def solution(record): | |
idWithAction = [] | |
idWithNickname = {} | |
for sentence in record: | |
log = resolve(sentence) | |
idWithAction.append((log['uid'], log['action'])) | |
if log['nickname'] != None: | |
idWithNickname[log['uid']] = log['nickname'] | |
answer = [] | |
for log in idWithAction: | |
uid = log[0] | |
nickname = idWithNickname[uid] | |
action = log[1] | |
if action == ENTER: | |
action = ENTER_KOREAN | |
elif action == LEAVE: | |
action = LEAVE_KOREAN | |
else: | |
action = None | |
if action is not None: | |
answer.append(nickname+'님이'+' '+action+'.') | |
return answer | |
def resolve(sentence): | |
arr = sentence.split() | |
action = arr[0] | |
nickname = None | |
if action == ENTER or action == CHANGE: | |
nickname = arr[2] | |
result = { | |
'uid': arr[1], | |
'nickname': nickname, | |
'action': arr[0] | |
} | |
return result | |
t1 = ["Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan"] | |
print(solution(t1)) # ["Prodo님이 들어왔습니다.", "Ryan님이 들어왔습니다.", "Prodo님이 나갔습니다.", "Prodo님이 들어왔습니다."] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment