Skip to content
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

Merged
merged 2 commits into from
Sep 15, 2024
Merged

57-xxubin04 #215

merged 2 commits into from
Sep 15, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Aug 8, 2024

πŸ”— 문제 링크

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - μ˜€ν”ˆμ±„νŒ…λ°©


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ + a


✨ μˆ˜λ„ μ½”λ“œ

1. 문제 이해

λ‹‰λ„€μž„ λ³€κ²½λ°©λ²•μ—λŠ” 2가지가 μ‘΄μž¬ν•œλ‹€.

  • μ±„νŒ…λ°©μ„ λ‚˜κ°„ ν›„, μƒˆλ‘œμš΄ λ‹‰λ„€μž„μœΌλ‘œ λ‹€μ‹œ 듀어감
  • μ±„νŒ…λ°©μ— μ†ν•΄μžˆλŠ” μƒνƒœμ—μ„œ λ‹‰λ„€μž„ λ³€κ²½ -> 기쑴의 λ‹‰λ„€μž„λ„ ν•¨κ»˜ λ³€κ²½λœλ‹€.

  • 쀑볡 λ‹‰λ„€μž„ ν—ˆμš©

예λ₯Ό 듀어보면, λ‹€μŒκ³Ό κ°™λ‹€.

  1. Prodoλ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€. (uid1234, Muziκ°€ μ±„νŒ…λ°© λ“€μ–΄μ˜΄)
  2. Ryanλ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€. (uid4567, Prodoκ°€ μ±„νŒ…λ°© λ“€μ–΄μ˜΄)
  3. Prodoλ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€. (uid1234, Muziκ°€ λ‹‰λ„€μž„μ„ Prodo둜 λ°”κΎΈκ³  λ‚œ 후에 μ±„νŒ…λ°© λ‚˜κ°)
  4. Prodoλ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€. (uid1234, Muzi(Prodo)κ°€ μ±„νŒ…λ°© λ“€μ–΄μ˜΄)
  5. (uid4567, Prodoκ°€ Ryan으둜 λ‹‰λ„€μž„ λ°”κΏˆ)

즉, κ°€μž₯ λ§ˆμ§€λ§‰μ— 정해진 λ‹‰λ„€μž„μœΌλ‘œ λͺ¨λ“  μ±„νŒ…λ°© λ©”μ‹œμ§€κ°€ λ³€ν™˜λœλ‹€.
κ·Έλž˜μ„œ μœ μ € 아이디λ₯Ό key둜 ν•˜μ—¬ (행동, λ‹‰λ„€μž„)을 μ €μž₯ν•œλ‹€. 그리고 κ°€μž₯ λ§ˆμ§€λ§‰μ— μ €μž₯된 (행동, λ‹‰λ„€μž„)μ—μ„œ λ‹€μŒμ˜ 쑰건을 λ”°μ Έ 각각의 μœ μ € 아이디에 ν•˜λ‚˜μ˜ λ‹‰λ„€μž„λ§Œμ„ μ •ν•΄μ€€λ‹€.

κ°€μž₯ λ§ˆμ§€λ§‰μ— μ €μž₯된 νŠœν”Œμ—μ„œ 행동이 Enterκ±°λ‚˜ Change의 λ‹‰λ„€μž„ = ν™•μ • λ‹‰λ„€μž„

ν•˜λ‚˜μ˜ λ‹‰λ„€μž„μ„ 각각 지정해주고 λ‚˜μ„œ Record에 λ‚˜μ˜€λŠ” μˆœμ„œλŒ€λ‘œ 값을 λ°˜ν™˜ν•΄μ€€λ‹€.



2. μ½”λ“œ 뢄석

    answer = []
    users = {}  # μ‚¬μš©μž 아이디λ₯Ό κΈ°μ€€μœΌλ‘œ (행동, λ‹‰λ„€μž„) μ €μž₯
    
    for r in record:  
        splitRecord = r.split()  # 곡백을 κΈ°μ€€μœΌλ‘œ λ‚˜λˆ μ€Œ ("Enter", "uid1234", "Muzi")
        command = splitRecord[0]  # 행동
        userId = splitRecord[1]  # μ‚¬μš©μž 아이디
        
        if command == "Leave":  # 행동이 Leave라면 λ‹‰λ„€μž„μ΄ 주어지지 μ•ŠμœΌλ―€λ‘œ λ”°λ‘œ 확인해쀀닀.
            if userId in users:   # (행동, λ‹‰λ„€μž„) νŠœν”Œμ„ μΆ”κ°€ν•΄μ€€λ‹€.
                users[userId].append((command, ""))
            else:
                users[userId] = [(command, "")]
        else:
            if userId in users:
                users[userId].append((command, splitRecord[2]))
            else:
                users[userId] = [(command, splitRecord[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")]]]

μœ„μ™€ 같이 μ €μž₯λœλ‹€.


    nicknames = {}

    for userId, action in users.items():  
        for command, name in reversed(action):  # users에 μ €μž₯된 각각의 μ‚¬μš©μž μ•„μ΄λ””μ˜ value인 νŠœν”Œ
            if command == "Enter" or command == "Change":  # λ§Œμ•½ 행동이 Enterλ‚˜ Change라면 λ‹‰λ„€μž„ ν™•μ •
                nicknames[userId] = name
                break

νŠœν”Œμ„ μ—­μˆœμœΌλ‘œ μˆœνšŒν•˜λ©΄μ„œ 행동이 Enterλ‚˜ Change인 κ²½μš°μ—λŠ” κ·Έλ•Œμ˜ λ‹‰λ„€μž„μœΌλ‘œ 결정짓고 for문을 νƒˆμΆœν•œλ‹€. (μ΅œμ‹ μ˜ λ‹‰λ„€μž„μœΌλ‘œ 확정짓기 μœ„ν•¨)

nicknames에 각각의 μ‚¬μš©μž 아이디에 따라 λ‹‰λ„€μž„μ„ μ €μž₯ν•΄μ€€λ‹€.


def msg(act, name):
    if act == "Enter":
        return name+"λ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€."
    else:  # act == "Leave"
        return name+"λ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€."
    for output in record:
        splitOutput = output.split()
        if splitOutput[0] != "Change":  # 행동이 Changeκ°€ μ•„λ‹ˆλΌλ©΄,
            answer.append(msg(splitOutput[0], nicknames[splitOutput[1]]))

record μˆœμ„œλŒ€λ‘œ answerλ₯Ό λ°˜ν™˜ν•΄μ•Ό ν•˜λ―€λ‘œ record의 각 λ¬Έμžμ—΄μ„ output으둜 λ°›μ•„ 곡백을 κΈ°μ€€μœΌλ‘œ splitν•΄μ€€λ‹€.
행동이 Changeκ°€ μ•„λ‹ˆλΌλ©΄, msgν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ€€λ‹€.


msgν•¨μˆ˜

행동이 Enter라면, return name+"λ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€."
행동이 Leave라면, return name+"λ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€."



3. 전체 μ½”λ“œ

 def msg(act, name):
    if act == "Enter":
        return name+"λ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€."
    else:  # act == "Leave"
        return name+"λ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€."


def solution(record):
    answer = []
    users = {}

    for r in record:
        splitRecord = r.split()
        command = splitRecord[0]
        userId = splitRecord[1]

        if command == "Leave":
            if userId in users:
                users[userId].append((command, ""))
            else:
                users[userId] = [(command, "")]
        else:
            if userId in users:
                users[userId].append((command, splitRecord[2]))
            else:
                users[userId] = [(command, splitRecord[2])]

    nicknames = {}

    for userId, action in users.items():
        for command, name in reversed(action):
            if command == "Enter" or command == "Change":
                nicknames[userId] = name
                break

    for output in record:
        splitOutput = output.split()
        if splitOutput[0] != "Change":
            answer.append(msg(splitOutput[0], nicknames[splitOutput[1]]))

    return answer

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš°λ¦¬μ—κ²Œ ν•„μš”ν•œ 건 "μ΅œμ’…μ μΈ λ‹‰λ„€μž„μœΌλ‘œ μœ μ €κ°€ λ“€μ–΄μ˜€κ³  λ‚˜κ°„ 기둝"μ΄μž–μ•„μš”? 즉 μš°λ¦¬λŠ”

  1. recordλ₯Ό ν•œ 번 순차적으둜 훑어보며 각 μœ μ € 별 μ΅œμ’… λ‹‰λ„€μž„μ„ 기둝
  2. 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')]

Copy link
Collaborator

@mjj111 mjj111 left a 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)

@9kyo-hwang 9kyo-hwang merged commit febe963 into AlgoLeadMe:main Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants