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

[Python/프로그래머스]뉴스 클러스터링_(KaKao)_Counter이용

개발자 덕구🐾 2022. 9. 26. 17:03
728x90

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/17677

 

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

교집합일 때는 min을, 합집합일 때는 max로 하는 걸 어떻게 하지 고민하다가 너무 답답해서 구글링한 문제

그냥 Counter에서 &, |를 하면 해결된단다.....

 

그 후에 counter에서 elements를 통해서 문자들을 개수 만큼 만들고 그것의 개수를 센다. 

 

 

코드 : 

from collections import Counter

def solution(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    
    str1_lst = []
    str2_lst = []

    for i in range(1,len(str1)) :
        tmp = str1[i-1:i+1]
        if tmp.isalpha() : str1_lst.append(tmp)
        
    for i in range(1,len(str2)) :
        tmp = str2[i-1:i+1]
        if tmp.isalpha() : str2_lst.append(tmp)
        
    Counter1 = Counter(str1_lst)
    Counter2 = Counter(str2_lst)
    
    
    inter = list((Counter1 & Counter2).elements())
    union = list((Counter1 | Counter2).elements())
    
    if len(union) ==0 and len(inter) == 0 :
        return 65536
    else :
        return int(len(inter) / len(union)*65536)

 

 

 

 

 

 

 

 

 

 

참고 블로그 : 

https://dongdongfather.tistory.com/70

 

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

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

dongdongfather.tistory.com

 

https://velog.io/@godiva7319/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-Level2-1%EC%B0%A8-%EB%89%B4%EC%8A%A4-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0%EB%A7%81-Python

 

프로그래머스 Level2 [1차] 뉴스 클러스터링 Python

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

velog.io

 

반응형