[프로그래머스] 완주하지 못한 선

rhkr9080·2023년 12월 2일
0

프로그래머스

목록 보기
8/19

문제링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42576

💻 문제 풀이 : Python

⭐ 일반 해쉬 사용법
def solution(participant, completion):
    answer = ''
    temp = 0
    dic = {}
    for part in participant:
        dic[hash(part)] = part
        temp += int(hash(part))
    for com in completion:
        temp -= hash(com)
    # 유일하게 하나의 멤버만 존재하므로 가능한 방법인듯...!
    answer = dic[temp]
    return answer

⭐ collections 클래스의 Counter 메서드 사용법

import collections

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)
    # print(collections.Counter(participant))
    # print(collections.Counter(completion))
    # print(This is hash of my key : answer)

    return list(answer.keys())[0]

📌 memo

  • hash() : 임의의 값을 해쉬 함수로 만들어 주는 것
str = "This is my key!!"
key = hash(str)
print(key)
----------------------------
출력 : 
3117656213745715764

❓Hash값으로 어떻게 만들까

  • 런타임마다 hash 값이 달라진다...!
  • TBD...
  • Counter : key - 배열의 값 / value - 해당 값의 출현 횟수
print(collections.Counter(participant))
print(collections.Counter(completion))
print(answer)
------------------------------------------
출력 : 
Counter({'leo': 1, 'kiki': 1, 'eden': 1})
Counter({'eden': 1, 'kiki': 1})
Counter({'leo': 1})
profile
공부방

0개의 댓글