카테고리 없음

프로그래머스 뉴스 클러스터링

soo_book 2020. 7. 3. 14:36
import re
from collections import Counter
def solution(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    
    A = []
    B = []

#   집합 A, B에 2문자씩 잘라서 저장

    p = re.compile("[a-z]{2}")
    for i in range(0, len(str1)-1):
        if p.match(str1[i:i+2]):
            A.append(str1[i:i+2])
    for i in range(0, len(str2)-1):
        if p.match(str2[i:i+2]):
            B.append(str2[i:i+2])


    print("A:",A)    
    print("B:",B)
    
    if len(A) == 0 and len(B) == 0:
        return 65536
    
    str1_cnt = Counter(A)
    str2_cnt = Counter(B)
    
    inter = len(list((str1_cnt & str2_cnt).elements()))
    union = len(A) + len(B) - inter
    return int(inter/union*65536)