Skip to content

Instantly share code, notes, and snippets.

@rpf5573
Last active April 4, 2019 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpf5573/b784a0f863fb0b25a15f88732c975535 to your computer and use it in GitHub Desktop.
Save rpf5573/b784a0f863fb0b25a15f88732c975535 to your computer and use it in GitHub Desktop.
카카오 오픈채팅방 알고리즘 코드
# -*- 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