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

 

문제

from collections import Counter

def intersection(ls1, ls2):
    if not ls1 and not ls2:
        return 1
    cnt1 = Counter(ls1)
    cnt2 = Counter(ls2)
    inter = cnt1 & cnt2
    return sum(inter.values())

def union(ls1, ls2):
    if not ls1 and not ls2:
        return 1
    cnt1 = Counter(ls1)
    cnt2 = Counter(ls2)
    inter = cnt1 | cnt2
    return sum(inter.values())
def solution(str1, str2):
    answer = 0
    str1 = str1.upper()
    str2 = str2.upper()
    
    ls1 = []
    ls2 = []
    
    for i in range(1, len(str1)):
        if str1[i-1:i+1].isalpha():
            ls1.append(str1[i-1:i+1])
        
    for i in range(1, len(str2)):
        if str2[i-1:i+1].isalpha():
            ls2.append(str2[i-1:i+1])
    return int(intersection(ls1, ls2) / union(ls1, ls2) * 65536)