왕바보같은 첫 코드
그냥 문제 보고 차집합인 줄 알았음
def solution(participant, completion):
str_list = [x for x in participant if x not in completion]
answer = str_list
return answer
그랬더니 테스트케이스 3번에서 걸리더라
ㅇㅎ 그래 맞아 동명이인이 있을 수 있지
하고 예전 문제들 뒤적거리다 생각난게 파이썬 집합 패키지.. Counter 가 있었다
Counter 는 이렇게 쓰는 애다
from collections import Counter
#중복이 있는 리스트를 만듬
fruits = ['apple', 'banana', 'orange', 'apple', 'banana', 'apple']
#카운더에 담음
fruit_count = Counter(fruits)
#dict 형태로 나옴 (해시)
print(fruit_count)
# Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})
#kye 느낌으로 접근 가능.. 개수세어줌
print(fruit_count['apple'])
# Output: 3
print(fruit_count.most_common(2))
# Output: [('apple', 3), ('banana', 2)]
여튼 그거 써서 풀면 사칙 연산이 아주 이지 ~
from collections import Counter
def solution(participant, completion):
c1, c2 = Counter(participant), Counter(completion)
answer = list(c1 - c2)[0]
return answer
728x90
'코딩테스트 > python' 카테고리의 다른 글
[python][프로그래머스_lv1] 숫자 짝꿍 (feat. sort 안 쓰기) (0) | 2023.03.23 |
---|---|
[python][프로그래머스_lv1] 체육복 (feat. 탐욕법) (0) | 2023.01.06 |
[python][프로그래머스_lv1] 로또의 최고 순위와 최저 순위 (0) | 2023.01.05 |
[python][코딩테스트_lv1] 다트 게임 (0) | 2022.12.30 |
[python][프로그래머스_lv1] 푸드 파이트 대회 (0) | 2022.12.30 |