09009

[프로그래머스 lv2] [1차] 뉴스 클러스터링 본문

Algorithm
[프로그래머스 lv2] [1차] 뉴스 클러스터링
09009

문제 보기

 

참고: https://dongdongfather.tistory.com/70

 

[파이썬 기초] Counter를 이용한 항목 계산

파이썬에서 항목의 개수를 셀때 사용하는 클래스로 Counter라는게 있다. 리스트나 셋을 인자로 넘기면 각 항목을 키로 해서 개수를 알려준다. 기본사용법은 이렇다. >>> from collections import Counter >>>

dongdongfather.tistory.com

문제 해결

from collections import Counter
def solution(str1, str2):
    answer = 0
    
    str1, str2 = str1.lower(), str2.lower()
    list_str1, list_str2 = [], []
    
    for i in range(len(str1)-1):
        if str1[i:i+2].isalpha():
            list_str1.append(str1[i:i+2])
            
    for i in range(len(str2)-1):
        if str2[i:i+2].isalpha():
            list_str2.append(str2[i:i+2])            
    
    counter_1 = Counter(list_str1)
    counter_2 = Counter(list_str2)
    
    inner = list((counter_1 & counter_2).elements())
    union = list((counter_1 | counter_2).elements())
    
    if len(inner) == 0 and len(union) == 0:
        return 65536
    else:
        return int(len(inner) / len(union) * 65536)

 

'Algorithm' 카테고리의 다른 글

[Python] 변수 값 변경 참고 (swap)  (0) 2023.09.22
[백준 8979번] 올림픽  (0) 2023.09.16
에라토스테네스의 체  (0) 2023.09.12
[프로그래머스 lv2] 땅따먹기  (0) 2023.08.30
[프로그래머스 lv2] 메뉴 리뉴얼  (0) 2023.04.04