기억은 짧고 기록은 길다
[프로그래머스/Programmers] 완주하지 못한 선수 - Python 본문
Link
코딩테스트 연습 - 완주하지 못한 선수
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수
programmers.co.kr
Solution
해당 문제는 sort(), sorted()를 사용하면쉽게 해결할 수 있는 문제이다.
하지만 Counter(), hash()등으로도 간단하게 해결가능하며 더 효율적으로 해결할 수 있다.
필자는 이번 문제를 통해 Counter()끼리의 - 연산이 가능하다는것과 hash()의 활용법을 알게 되었다.
def solution(participant, completion):
answer = ''
p = sorted(participant)
c = sorted(completion)
for i in range(len(c)):
if p[i] != c[i]:
return p[i]
return p[len(p) - 1]
🔑 key point: sort(), sorted(), Counter(), hash()
📌 Tip: Counter() - Counter()
Other Solution
from collections import Counter
def solution(participant, completion):
answer = Counter(participant) - Counter(completion)
return list(answer.keys())[0]
def solution(participant, completion):
answer = ''
temp = 0
dic = {}
for p in participant:
dic[hash(p)] = p
temp += int(hash(p))
for c in completion:
temp -= hash(c)
answer = dic[temp]
return answer
'CodingTest > programmers' 카테고리의 다른 글
[프로그래머스/Programmers] 핸드폰 번호 가리기 - Python (0) | 2021.08.31 |
---|---|
[프로그래머스/Programmers] 하샤드 수 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 약수의 합 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 시저 암호 - Python (0) | 2021.08.31 |
[프로그래머스/Programmers] 수박수박수박수박수박수 - Python (0) | 2021.08.31 |
Comments