DALL-E 그림

 

완주하지 못한 한 선수를 찾아야 한다. 

마라톤에 참여한 선수들의 이름이 담긴 배열이 주어질 때, 

완주하지 못한 선수의 이름을 반환하도록 soloution 함수를 작성하자 

 

마라톤 경기에 참여한 선수의 수는 1명 이상, 100,000명 이하이다. 

completion의 길이는 participant의 길이보다 1 작다. 

참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있다. 

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

 

participant completion return
c, a, b b, c a

 

a를 찾으시오. 


 

1. sorting / loop을 활용

2. hash를 활용

3. collections.Counter 활용 

 

 

1. sorting 

두 리스트를 정렬하여 

completion list의 length만큼 돌면서 

participant에만 존재하는 한 명을 찾는다. 

def solution(participant, completion):
    # 1. Sort the lists
    participant.sort()
    completion.sort()

    # 2. Compare the lists
    for i in range(len(completion)):
        if participant[i] != completion[i]:
            return participant[i]

    return participant[len(participant) - 1]

print(solution(['leo', 'kiki', 'eden'], ['eden', 'kiki']))
leo

 

 

2. Hash :

해시 함수는 임의의 길이의 값을 입력받아 고정된 길이의 결과 값을 출력하는 함수. 

해시 함수에 의해 얻어지는 값을 해시 값이라 하고 간단하게 해시라고 한다고. 

Key Value
17 c
5 a
13 b


Hash를 key로 갖고, participant의 이름을 value로 갖는 Dictionary를 만들어 합을 구한다. sumHash: 35, 

합에서 completion의 hash들의 합을 뺀다. sumHash: 5

마지막에 남은 hash가 완주하지 못한 선수의 hash다. 5 → a

def solution(participant, completion):
    hashDict = {}
    sumHash = 0

    # 1. Hash the completion list
    for part in participant:
        hashDict[hash(part)] = part
        sumHash += hash(part)
    
    # 2. Subtract the hash of the completion list
    for comp in completion :
        sumHash -= hash(comp)
    
    # 3. Returen the remaining hash
    return hashDict[sumHash]

print(solution(['leo', 'kiki', 'eden'], ['leo', 'kiki']))
eden

 

 

3. Counter 

participant와 completion의 Counter를 구한다. 

둘의 차를 구하면 객체 1개 짜리 Counter를 반환한다. 

Counter의 Key 값을 return하면 됨 

 

participant = 

Key Value
c 1
a 1
b 1

 

completion = 

Key Value
c 1
b 1

 

participant - completion = 

Key Value
a 1
from collections import Counter

def solution(participant, completion):
    # 1. Count the occurrences of each participant
    # 2. Count the occurrences of each completer
    # 3. Calculate the difference between participant counts and completer counts to find the remaining participant
    # 4. Return the name of the remaining participant
    answer = Counter(participant) - Counter(completion)
    return list(answer.keys())[0]

print(solution(['leo', 'kiki', 'eden'], ['leo', 'eden']))
kiki

 

 

 

영상 보면서 내용 기억하려고 기록함.. 

https://youtu.be/cJ9xdW_hqR4?si=t8kdW3yrCJcWaln9

 

 

 

썸네일 하나 만들기 쉽지 않다.. 재밌긴 한데 넘 늦은 시간이라 피곤함
DALL.E가 그림. 트랙 무슨 일이야ㅋㅋㅋㅋㅋㅋ

 

+ Recent posts