프로그래머스_완주하지 못한 선수_python

2021. 7. 18. 21:30코딩테스트

반응형

 

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

 

코딩테스트 연습 - 완주하지 못한 선수

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수

programmers.co.kr

마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.

참가자 중에는 동명이인이 있을 수 있습니다.

 

collections의 counter 함수는 해당 키값이 몇번 나왔는지 확인함.

 

import collections

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

collections.Counter(arr) // Counter({'leo': 1, 'kiki': 1, 'eden': 1})

collections.Counter(arr2) // Counter({'leo': 1, 'kiki': 1})

answer = collections.Counter(arr) - collections.Counter(arr2) // Counter({'eden': 1})

answer.keys() //dict_keys(['eden'])

list(answer.keys()) //['eden']

반응형

'코딩테스트' 카테고리의 다른 글

프로그래머스_기능개발_python  (0) 2021.07.25
프로그래머스_베스트앨범_python  (0) 2021.07.21
프로그래머스_위장_python  (0) 2021.07.20
프로그래머스_전화번호 목록_python  (0) 2021.07.18
해시란?  (0) 2021.07.18