보물창고 블로그

2018 KAKAO BLINDRECRUITMENT 1차 뉴스 클러스터링 본문

알고리즘 풀이/프로그래머스

2018 KAKAO BLINDRECRUITMENT 1차 뉴스 클러스터링

홋 메 2020. 3. 26. 15:34
728x90

문제 링크: https://programmers.co.kr/learn/courses/30/lessons/17677

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

자카드 유사도를 구현하는 문제였다. collections모듈의 Counter함수를 사용하여 쉽게 풀었다. 먼저 각 문자열에 대해서 특수문자가 있는지를 확인하여 없으면 set1, set2에 2 글자씩 끊은 문자열을 넣어주고, set자료구조의 union과 intersection함수를 사용하여 교집합에 있는 단어의 경우 최대 빈도수를 maxv에 더하고 최소 빈도를 minv에 더하여 자카드 유사도를 구하였다. 아래는 내가 해결한 코드이다.

from collections import Counter


def solution(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    set1 = []
    set2 = []
    for i in range(len(str1) - 1):
        if 96 < ord(str1[i]) < 123 and 96 < ord(str1[i + 1]) < 123:
            set1.append(str1[i:i + 2])
    for j in range(len(str2) - 1):
        if 96 < ord(str2[j]) < 123 and 96 < ord(str2[j + 1]) < 123:
            set2.append(str2[j:j + 2])
    c1 = Counter(set1)
    c2 = Counter(set2)
    ming = set(set1).intersection(set(set2))
    maxg = set(set1).union(set(set2))
    minv = 0
    maxv = 0
    for word in maxg:
        # str1에 없을 때
        if c1.get(word) is None:
            maxv += c2.get(word)
        elif c2.get(word) is None:
            maxv += c1.get(word)
        else:
            if c1.get(word) > c2.get(word):
                minv += c2.get(word)
                maxv += c1.get(word)
            else:
                minv += c1.get(word)
                maxv += c2.get(word)
    if maxv == 0:
        return 65536
    else:
        return int(minv / maxv * 65536)
Comments