coding test - python/Programmers
Programmers / 완주하지 못한선수 / Python
sillon
2022. 4. 7. 19:56
728x90
반응형
*문제 출처는 프로그래머스에 있습니다.
문제 제목: 완주하지 못한선수
문제 사이트: https://programmers.co.kr/learn/courses/30/lessons/42576
나의 풀이(틀림)
def solution(participant, completion):
new_list = list(set(participant) - set(completion))
for x in new_list:
return x
나의 함수는 동명이인이 있는 경우는 고려되지 않는 잘못된 함수이다..
다른 풀이
def solution(participant, completion):
participant.sort()
completion.sort()
for p,c in zip(participant, completion):
if p != c:
return p
return participant.pop()
* zip함수는 두 요소의 길이가 다르면 같은 것 끼리 짝지어준다.
다른 사람의 풀이에서 가장 좋아요를 많이 받은 답변은 collections였다.
import collections
def solution(participant, completion):
answer = collections.Counter(participant) - collections.Counter(completion)
return list(answer.keys())[0]
https://sulmasulma.github.io/algorithm/2020/05/12/collections.html
python collections 라이브러리 다루기
python 알고리즘을 풀 때 유용한 라이브러리 중 하나인 collections의 용도를 소개한다.
sulmasulma.github.io
※ 알아야 할 것
* zip함수는 두 요소의 길이가 다르면 같은 것 끼리 짝지어준다.
728x90
반응형