[Python] 프로그래머스-해시-완주하지 못한 선수

김준영·2022년 10월 31일
1

코딩테스트

목록 보기
1/22
from collections import Counter

def solution(participant, completion):
    p = dict(Counter(participant))
    c = dict(Counter(completion))
    
    for i in participant:
        try:
            if p[i] != c[i]:
                return i
        except KeyError:
                return i
  1. 파이썬 라이브러리 Collections.Counter를 사용하여 동명이인을 찾는다.
    • 동명이인이 있으면 Counter의 value값이 1이 아니다.
  2. 해시를 이용해야 하므로 dict로 변환해줌.
  3. 동명이인이 있는데 value값이 다르면 완주를 못했으므로 return 이름.
  4. KeyError는 완주명단에 없는 선수를 검색했을때 나오는 Error이므로 return 이름.
profile
ㅎㅎ

0개의 댓글