Programing Language/Python
Python 프로그래머스 : 해시 함수1
Data-SSung
2020. 7. 29. 13:29
반응형
문제
- 수많은 마라톤 선수들이 마라톤에 참여함
- 단, 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주함
- 마라톤에 참여한 선수들의 이름이 담긴 배열 participant
- 완주한 선수들의 이름이 담긴 배열 completion
완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성하기.
제약조건
- 선수의 수는 1명 이상 100,000명 이하
- completion의 길이는 participant의 길이보다 1 작음
- 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자
- 참가자 중에는 동명이인이 있을 수 있음
해결전략
1. list를 딕션너리화
2. 차집합 개념 활용
코드
# version 1
# test 통과 O
def solution(participant, completion):
import collections
# key값에 이름, value값에는 1이 담김
re = collecions.Counter(participant) - collections.Counter(completion)
return list(re.key)[0]
# versoin 2
# 효율성 통과 X
def solution(participant, completion):
test = participant.copy()
for i in completion:
if i in participant:
test.remove(i)
pass
return test[0]
참고 : 프로그래머스
url : https://programmers.co.kr/learn/challenges?selected_part_id=17046
반응형