- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42576
Level1 문제고, 스스로 푼 문제다!
participant 중에 마라톤을 완료하지 못한 한 명을 찾아 이름을 반환해야한다
1.
not in completion으로 풀 수도 있지만, 동명이인인 경우에 participant에 동명이인이 있고, 그 중 한명이 completion에 없다면 complete하지 못했는데 complete했다고 여겨질 수 있다
2.
그래서 이름에 해당하는 사람 수를 딕셔너리에 저장한 후 completion에서 이름이 있을 때마다 값을 -= 1 하는 방식으로 진행한 다음
3.
통과하지 못한 사람은 마지막에 키 값이 1일 것이므로 해당 키를 반환하면 된다
- 정답 풀이 :
def solution(participant, completion):
result = {}
answer = ''
for name in participant :
if name not in result:
result[name] = 1
else:
result[name] += 1
for name in completion :
if name in result:
if result[name] != 0 :
result[name] -= 1
for name in result:
if result[name] != 0 :
answer += name
return answer
- 다른 풀이 :
Counter 모듈을 이용해서 각 리스트의 차이를 구해 정답을 찾는 풀이다
from collections import Counter
def solution(participant, completion):
result = Counter(participant) - Counter(completion)
return list(result.keys())[0]
'프로그래머스 > Level1' 카테고리의 다른 글
[Summer/Winter Coding(~2018) / 프로그래머스] 12982번 : 예산 (0) | 2022.08.24 |
---|---|
[월간 코드 챌린지 시즌 1 / 프로그래머스] 68935번 : 3진법 뒤집기 (0) | 2022.08.24 |
[2019 KAKAO BLIND RECRUITMENT/ 프로그래머스] 42889번 : 실패율 (1) | 2022.08.23 |
[완전탐색 / 프로그래머스] 42840번 : 모의고사 (0) | 2022.08.20 |
[정렬 / 프로그래머스] 42748번 : K번째 수 (0) | 2022.08.20 |