[프로그래머스_17677] 뉴스 클러스터링 Python 풀이

출처: 프로그래머스

문제

image image

풀이

Counter 모듈을 이용하여 구현해주었습니다.

set을 이용하여 교집합(intersection) 또는 합집합(union)을 구해줄 수 도 있지만, set은 중복을 제거하기 때문에 이 문제와는 맞지 않았습니다.

각각의 단어의 두 글자가 알파벳인지 확인해준 뒤 대문자로 변환해준 뒤 list에 넣어주었습니다.

각각의 단어의 list를 Counter로 바꿔준 뒤, intersection(‘&’), union(‘|‘)을 해준 Counter의 element 개수를 저장해주었습니다. 문제에서 지시한대로 intersection에 65536을 곱한 값을 union으로 나눠주었습니다.

코드

from collections import Counter
def solution(str1, str2):
    answer = 0
    s1 = []
    s2 = []
    for i in range(len(str1)-1):
        if str1[i].isalpha() and str1[i+1].isalpha():
            s1.append(str1[i:i+2].upper())
    for j in range(len(str2)-1):
        if str2[j].isalpha() and str2[j+1].isalpha():
            s2.append(str2[j:j+2].upper())
    cnt1 = Counter(s1)
    cnt2 = Counter(s2)
    intersection = len(list((cnt1 & cnt2).elements()))
    union = len(list((cnt1 | cnt2).elements()))
    return 65536*intersection // union if union else 65536

Leave a comment