문제풀이/프로그래머스

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

얄루몬 2021. 10. 6. 17:43

< 오답 1 >

def solution(participant, completion):
    answer = []
    for i in participant:
        if completion not in participant:
            answer.append(i)
    
    return answer

# 이렇게 진행하면 리스트형으로 출력을 해서 안 된다.

 

 

< 오답 2 >

def solution(participant, completion):
    answer = []
    for i in participant:
        if completion not in participant:
            answer += i
    return answer

# 이렇게 진행하면 중복값에 문제가 생기게 된다. 2명의 참가자 중 1명이 통과를 해도 1명이 통과해서 2명 전부 통과한 걸로 나오기 때문에 문제가 생긴다. 

 

# 효율성에서도 0점이 나옴 ㅋ;;; 

 

< 정답 1 >

def solution(participant, completion):
    participant.sort()
    completion.sort()
    for p,c in zip(participant,completion):
        if p != c:
            return p
    return participant.pop()

# zip 함수를 통해서 정렬시켜 놓은 리스트의 값들을 비교하며 같은 값이 아닐 경우 그 값을 돌려주어 진행.

 

< 정답 2 >

import collections


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

# Collections 모듈을 사용해서 Counter 객체를 이용해 빼주고 그것을 돌려주는 방식으로 진행 원래는 딕셔너리끼리도 빼기는 안 된다고 한다.