Skip to content

[seungbinpark] programmers_완주하지 못한 선수_Python - #7

Open
seungbinpark wants to merge 4 commits into
sejongbinary:mainfrom
seungbinpark:seungbinpark-patch-3
Open

[seungbinpark] programmers_완주하지 못한 선수_Python#7
seungbinpark wants to merge 4 commits into
sejongbinary:mainfrom
seungbinpark:seungbinpark-patch-3

Conversation

@seungbinpark

Copy link
Copy Markdown
Collaborator

🔗 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/42576

문제

수많은 마라톤 선수 중 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주함

  • 마라톤에 참여한 선수들의 이름이 담긴 배열 'participant'
  • 완주한 선수들의 이름이 담긴 배열 'completion'

완주하지 못한 선수의 이름을 return

<제한사항>

  • 마라톤 경기에 참여한 선수의 수 N은 1<=N<=100,000
  • len(completion) = len(participant) - 1
  • 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자
  • 참가자 중에는 동명이인이 있을 수 있다.

💡 풀이 아이디어

  • 완주한 사람 리스트의 각 이름 카운트해 딕셔너리 c에 저장
  • 참가자 리스트와 c의 카운트를 대조하며 완주 여부 확인
  • c에 없거나 c["이름"]=0인데 이름이 또 나온 경우 -> 완주X

처음에는 마지막 제한사항을 확인하지 못하고 코드를 작성했다.

def solution(participant, completion):
    ans = list(set(participant)-set(completion))
    return ans[0]

아래는 딕셔너리를 활용해 작성한 코드이다.

from collections import Counter
def solution(participant, completion):
    ans=[] # 완주하지 못한 사
    
    # completion 리스트의 각 이름을 카운트해 딕셔너리 형태로 저장
    c = Counter(completion)
    
    # participant 리스트 완주 여부 확인
    for name in participant:
        if c[name]>0:   # name이 completion에 존재하는 경우
            c[name] -= 1   # name의 카운트 -1
        else:
            ans.append(name)   # completion에 없거나 이미 0인 이름을 결과 리스트에 추가
            
    return ans[0]

📝 새로 학습한 내용

'Counter'는 파이썬의 'collections' 모듈에서 제공하는 클래스이다. 이는 해당 요소가 몇 번 등장했는지 세는 데 특화된 딕셔너리 형태의 자료구조이다.

<예>
'Counter(['a','b','b'])' -> '{'a' : 2, 'b': 1}'

Counter() 풀이

from collections import Counter

def solution(participant, completion):
    dict_result=Counter(participant)-Counter(completion)
    return list(dict_result.keys())[0]  

딕셔너리의 뺄셈을 이용하여 남은 'participant'의 key값이 완주하지 못한 선수가 되어 return하면 된다.

📚 참고 자료

@seungbinpark seungbinpark self-assigned this Jan 12, 2025
@just-stopyoon just-stopyoon added 🪄Programmers 프로그래머스 문제 풀이 🫧Python 풀이 언어 (Python) labels Jan 13, 2025
@just-stopyoon just-stopyoon changed the title Seungbinpark patch 3 [Seungbinpark] programmers_완주하지 못한 선수_Python Jan 13, 2025

@just-stopyoon just-stopyoon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

주석까지 달아주신다면 코드가 훨씬 가독성이 좋지 않을까... 생각합니닷! 'ㅅ'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

무궁무진한 파이썬 라이브러리의 세계 ... (역시 파이썬은 신이야)
새로운 클래스 Counter에 대해서 알 수 있었고, 이를 사용하니 훨씬 코드를 간결하게 작성할 수 있는 것 같아요!

@just-stopyoon just-stopyoon changed the title [Seungbinpark] programmers_완주하지 못한 선수_Python [seungbinpark] programmers_완주하지 못한 선수_Python Jan 19, 2025

@sangkyu39 sangkyu39 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

counter 클래스를 활용하면 몇 번 나왔는지 확인하는 문제에서 다양하게 활용할 수 있을 것 같아서 좋네요!! 이번에 새로 알아갑니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🪄Programmers 프로그래머스 문제 풀이 🫧Python 풀이 언어 (Python)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants