코딩문제풀이/프로그래머스
[Python] 완주하지 못한 선수
코딩하는 포메라니안
2021. 8. 23. 16:04
1. 문제
2. 풀이 과정
방법 1) 아스키코드를 기준으로 정렬한 후, 앞에서부터 순차 탐색한다.
def solution(participant, completion):
participant.sort()
completion.sort()
answer = ''
for i in range(len(completion)):
if participant[i] != completion[i]:
answer = participant[i]
break
if answer == '':
answer = participant[-1]
return answer
방법 2) collections 라이브러리 활용하기
collections.Counter(배열) => 사전 형태로 {'배열 속의 데이터' : 개수, ... } 를 반환한다.빼기 연산을 수행하면, 같은 키값을 가진 것끼리 빼기 연산이 수행되며 개수가 0인 것은 사전에서 삭제된다.따라서, 빼기 후 하나 남은 key값을 출력하면 된다.
import collections
def solution(participant, completion):
answer = collections.Counter(participant)-collections.Counter(completion)
return list(answer.keys())[0]