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