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

[프로그래머스] 파이썬 문제풀이 - [1차] 뉴스 클러스터링

by whdgus928 2023. 5. 23.

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

 

프로그래머스

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

programmers.co.kr

내 풀이

1. 대문자를 소문자로 바꿔준다

2. 두 문자열을 탐색하며 두글자씩 끊어서 알파벳인경우만 리스트에 삽입한다

3. a와 b 리스트를 깊은 복사해준다

4. 다중집합에서 교집합을 구한다

5. 다중집합에서 합집합을 구한다

import math
def solution(str1, str2):
    str1=str1.lower()
    str2=str2.lower()

    a=[]
    b=[]
    for i in range(len(str1)-1):
        if (str1[i:i+2]).isalpha():
            a.append(str1[i:i+2])
    for i in range(len(str2)-1):
        if (str2[i:i+2]).isalpha():
            b.append(str2[i:i+2])

    gyo=0 #다중집합에서 교집합을 구하는 방법
    b1=b.copy()
    for i in a:
        if i in b1:
            gyo+=1
            b1.remove(i)
            
    hap=[] #다중집합에서 합집합을 구하는 방법
    a1=a.copy() 
    for i in b:
        hap.append(i)
        if i in a1:
            a1.remove(i)
    hap=a1+hap
    
    if gyo==0 and len(hap)==0:
        return 65536
    else: return math.floor(gyo/len(hap)*65536)

 

배운 점

1. 다중집합에서 합집합과 교집합을 구하는 방법을 구현해봤다

 

반응형

댓글