-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
57-xxubin04 #215
57-xxubin04 #215
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ°λ¦¬μκ² νμν 건 "μ΅μ’ μ μΈ λλ€μμΌλ‘ μ μ κ° λ€μ΄μ€κ³ λκ° κΈ°λ‘"μ΄μμμ? μ¦ μ°λ¦¬λ
- recordλ₯Ό ν λ² μμ°¨μ μΌλ‘ νμ΄λ³΄λ©° κ° μ μ λ³ μ΅μ’ λλ€μμ κΈ°λ‘
- recordλ₯Ό ν λ² λ μνν΄ Enter/Leaveμ λν΄ [μ΅μ’ λλ€μ]λμ΄ λ€μ΄μ/λκ°μ΅λλ€ λ¬Έμμ΄ λ§λ€κΈ°
μ΄ 2λ¨κ³λ₯Ό μννλ©΄ λ©λλ€. κ·Έλ¬λ©΄ μ½λκ° λ¨μν΄μ§μ£ .
# r.split()[0]: Enter/Leave/Change
# r.split()[1]: id
# r.split()[2]: nickname
id_to_nickname = {r.split()[1] : r.split()[2] for r in record if not r.startswith('Leave')}
μ°μ recordλ₯Ό μ°¨λ‘λλ‘ μνν΄ κ° μ μ λ³ μ΅μ’ μ μΈ λλ€μμ νλνλ€. Enter/Changeμ μν΄ λλ€μμ΄ κ²°μ λλ€.
message = {'Enter':'λμ΄ λ€μ΄μμ΅λλ€.', 'Leave':'λμ΄ λκ°μ΅λλ€.'}
λ¬Έμμ΄μ λ§λ€κΈ°μν΄ νμν λμ λ리λ₯Ό μ μΈν΄μ€λ€.
# r.split()[1]: id -> id_to_nickname[r.split()[1]]: idμ λμλλ μ΅μ’
λλ€μ
# r.split()[0]: Enter/Leave/Change -> message[r.split()[0]]: κ°κ°μ λμλλ λ¬Έμμ΄
return [f'{id_to_nickname[r.split()[1]]}{message[r.split()[0]]}' for r in record if not r.startswith('Change')]
μ΅μ’ μ μΌλ‘ μ°λ¦¬λ λ€μ΄μ¨/λκ° λ¬Έμμ΄μ΄ νμνλ―λ‘ Enter, Leaveμ λν΄ idμ λμλλ λλ€μκ³Ό messageλ₯Ό μ΄μ©ν΄ λ¬Έμμ΄μ λ§λ€μ΄ 리μ€νΈμ μ μ₯ν λ€ λ°ννλ€.
μ 체 μ½λ
def solution(record):
id_to_nickname = {r.split()[1] : r.split()[2] for r in record if not r.startswith('Leave')}
message = {'Enter':'λμ΄ λ€μ΄μμ΅λλ€.', 'Leave':'λμ΄ λκ°μ΅λλ€.'}
return [f'{id_to_nickname[r.split()[1]]}{message[r.split()[0]]}' for r in record if not r.startswith('Change')]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμ μ νμλ νμ΄μ¬ μ½λμ
λλ€λ§, μ΄μ€ forλ¬Έ νμμμ΄
O(n) μμ ν μ μμ΅λλ€..!
def solution(record):
answer = []
dic = {}
for sentence in record:
sentence_split = sentence.split()
if len(sentence_split) == 3:
dic[sentence_split[1]] = sentence_split[2]
for sentence in record:
sentence_split = sentence.split()
if sentence_split[0] == 'Enter':
answer.append(f'{dic[sentence_split[1]]}λμ΄ λ€μ΄μμ΅λλ€.' )
elif sentence_split[0] == 'Leave':
answer.append(f'{dic[sentence_split[1]]}λμ΄ λκ°μ΅λλ€.')
return(answer)
π λ¬Έμ λ§ν¬
νλ‘κ·Έλλ¨Έμ€ - μ€νμ±ν λ°©
βοΈ μμλ μκ°
1μκ° + a
β¨ μλ μ½λ
1. λ¬Έμ μ΄ν΄
λλ€μ λ³κ²½λ°©λ²μλ 2κ°μ§κ° μ‘΄μ¬νλ€.
μλ₯Ό λ€μ΄λ³΄λ©΄, λ€μκ³Ό κ°λ€.
μ¦, κ°μ₯ λ§μ§λ§μ μ ν΄μ§ λλ€μμΌλ‘ λͺ¨λ μ±ν λ°© λ©μμ§κ° λ³νλλ€.
κ·Έλμ μ μ μμ΄λλ₯Ό keyλ‘ νμ¬
(νλ, λλ€μ)
μ μ μ₯νλ€. κ·Έλ¦¬κ³ κ°μ₯ λ§μ§λ§μ μ μ₯λ(νλ, λλ€μ)
μμ λ€μμ 쑰건μ λ°μ Έ κ°κ°μ μ μ μμ΄λμ νλμ λλ€μλ§μ μ ν΄μ€λ€.νλμ λλ€μμ κ°κ° μ§μ ν΄μ£Όκ³ λμ Recordμ λμ€λ μμλλ‘ κ°μ λ°νν΄μ€λ€.
2. μ½λ λΆμ
record
= ["Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan"]users
= ["uid1234" : [("Enter", "Muzi"), ("Leave", ""), ("Enter", "Prodo")], "uid4567" : [("Enter", "Prodo"), ("Change", "Ryan")]]]μμ κ°μ΄ μ μ₯λλ€.
ννμ μμμΌλ‘ μννλ©΄μ νλμ΄ Enterλ ChangeμΈ κ²½μ°μλ κ·Έλμ λλ€μμΌλ‘ κ²°μ μ§κ³ forλ¬Έμ νμΆνλ€. (μ΅μ μ λλ€μμΌλ‘ νμ μ§κΈ° μν¨)
nicknamesμ κ°κ°μ μ¬μ©μ μμ΄λμ λ°λΌ λλ€μμ μ μ₯ν΄μ€λ€.
record μμλλ‘ answerλ₯Ό λ°νν΄μΌ νλ―λ‘ recordμ κ° λ¬Έμμ΄μ outputμΌλ‘ λ°μ 곡백μ κΈ°μ€μΌλ‘ splitν΄μ€λ€.
νλμ΄ Changeκ° μλλΌλ©΄, msgν¨μλ₯Ό νΈμΆν΄μ€λ€.
msgν¨μ
νλμ΄ EnterλΌλ©΄,
return name+"λμ΄ λ€μ΄μμ΅λλ€."
νλμ΄ LeaveλΌλ©΄,
return name+"λμ΄ λκ°μ΅λλ€."
3. μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©