본문 바로가기
Algorithm/프로그래머스

[프로그래머스] - 뉴스 클러스터링 (파이썬)

by 2HS 2022. 1. 30.
def solution(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    one = [str1[i:i+2] for i in range(len(str1)-1) if str1[i:i+2].isalpha()]
    two = [str2[i:i+2] for i in range(len(str2)-1) if str2[i:i+2].isalpha()]
    print(one)
    print(two)

    one_and_two = set(one) & set(two)
    one_or_two = set(one) | set(two)

    and_num = 0
    or_num = 0
    for i in one_and_two:
        and_num += min(one.count(i), two.count(i))
    for i in one_or_two:
        or_num += max(one.count(i), two.count(i))

    if or_num == 0:
        return 65536
    else:
        return int(and_num / or_num * 65536)

 

2018 카카오 블라인드 1차 - 5번

1. one과 two리스트를 str1과 str2를 이용해서  알파벳인 두쌍으로 초기화

2. set의 합집합과 교집합을 실행 (set는 중복을 허용하지 않아서 여기서 중복된 값을 빼고 결과가 나옴)

3. set때문에 사라진 중복된 값을 고려하기 위해 one과 two에서 해당 값의 개수를 센다

    - 교집합은 두 리스트 중에 가장 작은 개수를 더한다

    - 합집합은 두 리스트 중에 가장 큰 개수를 다한다

4. 합집합의 크기가 0이면 1로 계산, return