https://school.programmers.co.kr/learn/courses/30/lessons/42576
"""
입출력 예시)
participant = ["leo", "kiki", "eden"], completion = ["eden", "kiki"] -> "leo"
participant = ["marina", "josipa", "nikola", "vinko", "filipa"], completion = ["josipa", "filipa", "marina", "nikola"] -> "vinko"
participant = ["mislav", "stanko", "mislav", "ana"], completion = ["stanko", "ana", "mislav"] -> "mislav"
"""
def solution(participant, completion):
answer = ''
for p in participant:
if p not in completion:
answer = p
return answer
-> 내가 푼 방법으로는 참가자 중에 동명이인이 있을 경우에 대해 처리하지 못함...
- 방법 1) collections.Counter() 사용
from collections import Counter
def solution(participant, completion):
answer = list((Counter(participant) - Counter(completion)).keys())[0]
return answer
- Counter(participant), Counter(completion), list((Counter(participant) - Counter(completion)).keys())) 확인
# participant = ["mislav", "stanko", "mislav", "ana"]
# completion = ["stanko", "ana", "mislav"]
"""
Counter(participant): Counter({'mislav': 2, 'stanko': 1, 'ana': 1})
Counter(completion): Counter({'stanko': 1, 'ana': 1, 'mislav': 1})
list((Counter(participant) - Counter(completion)).keys())): ['mislav']
"""
- collections.Counter() 설명 참고
https://velog.io/@kimdukbae/Python-collections-%EB%AA%A8%EB%93%88%EC%9D%98-Counter
def solution(participant, completion):
answer = ''
temp = 0
dic = {}
for p in participant:
dic[hash(p)] = p # dic = {hash(p): 'p'}
temp += hash(p) # temp = temp + hash(p)
for c in completion:
temp -= hash(c) # temp = temp - hash(c)
answer = dic[temp]
return answer
-> 동일한 'misalv'라는 이름은 participant 리스트에 있으나 completion 리스트에 있으나 같은 hash 값을 가진다는 hash의 특징 활용
# participant = ["mislav", "stanko", "mislav", "ana"]
# completion = ["stanko", "ana", "mislav"]
"""
p: mislav
hash(p): -1328427996545938244
dic: {-1328427996545938244: 'mislav'}
temp: -1328427996545938244
p: stanko
hash(p): -4704244479607317033
dic: {-1328427996545938244: 'mislav', -4704244479607317033: 'stanko'}
temp: -6032672476153255277
p: mislav
hash(p): -1328427996545938244
dic: {-1328427996545938244: 'mislav', -4704244479607317033: 'stanko'}
temp: -7361100472699193521
p: ana
hash(p): -8358854611270094177
dic: {-1328427996545938244: 'mislav', -4704244479607317033: 'stanko', -8358854611270094177: 'ana'}
temp: -15719955083969287698
_____________________________________________________________________________________________________________
c: stanko
hash(c): -4704244479607317033
temp: -11015710604361970665
c: ana
hash(c): -8358854611270094177
temp: -2656855993091876488
c: mislav
hash(c): -1328427996545938244
temp: -1328427996545938244
_____________________________________________________________________________________________________________
dic: {-1328427996545938244: 'mislav', -4704244479607317033: 'stanko', -8358854611270094177: 'ana'}
"""
참고)
https://leedakyeong.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-Python-%EC%99%84%EC%A3%BC%ED%95%98%EC%A7%80-%EB%AA%BB%ED%95%9C-%EC%84%A0%EC%88%98