https://programmers.co.kr/learn/courses/30/lessons/42888
✅ Solution
- 딕셔너리를 이용해 유저 정보를 저장한다. (key: 유저 아이디, value: 닉네임)
- Enter 일 때는 E + 유저 아이디를 answer에 넣어준다.
- Leave 일 때는 L + 유저 아이디를 answer에 넣어준다.
- Change 일 때는 딕셔너리에서 해당 유저 아이디의 값을 새로운 닉네임으로 바꿔준다.
- Answer 전체를 돌면서 딕셔너리에 유저 아이디 값으로 접근해 닉네임을 받아와 출력한다.
✅ Code
def solution(records):
answer = []
names = {}
for record in records:
r = record.split()
if r[0] == 'Enter':
answer.append('E'+ r[1])
names[r[1]] = r[2]
elif r[0] == 'Leave':
answer.append('L' + r[1])
elif r[0] == 'Change':
names[r[1]] = r[2]
for i in range(len(answer)):
if answer[i][0] == 'E':
answer[i] = names[answer[i][1:]] + '님이 들어왔습니다.'
else:
answer[i] = names[answer[i][1:]] + '님이 나갔습니다.'
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - [1차] 캐시 (Python) (0) | 2022.05.17 |
---|---|
[프로그래머스] Level 2 - [1차] 프렌즈4블록 (Python) (0) | 2022.05.17 |
[프로그래머스] Level 2 - 튜플 (Python) (0) | 2022.05.13 |
[프로그래머스] Level 2 - 후보키 (Python) (0) | 2022.05.13 |
[프로그래머스] Level 2 - 메뉴 리뉴얼 (Python) (0) | 2022.05.13 |