일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 1789번
- 1038번
- 감소하는 수
- SW ExpertAcademy
- 어른 상어
- python
- 보석 쇼핑
- SW Expert Academy
- 베스트엘범
- 백준 알고리즘
- 17144번
- 9095번
- 14499번
- 12869번
- 빛의 경로 사이클
- 19238번
- 16234번
- HTML 기초
- 수식 최대화
- 프로그래머스
- 12865번
- 거울 설치
- 15686번
- 키패드 누르기
- 스타트 택시
- 미세먼지 안녕!
- 파이썬
- QueryDSL 기초
- 2020 카카오 인턴십
- 경주로 건설
Archives
- Today
- Total
보물창고 블로그
2018 KAKAO BLINDRECRUITMENT 1차 뉴스 클러스터링 본문
728x90
문제 링크: https://programmers.co.kr/learn/courses/30/lessons/17677
자카드 유사도를 구현하는 문제였다. collections모듈의 Counter함수를 사용하여 쉽게 풀었다. 먼저 각 문자열에 대해서 특수문자가 있는지를 확인하여 없으면 set1, set2에 2 글자씩 끊은 문자열을 넣어주고, set자료구조의 union과 intersection함수를 사용하여 교집합에 있는 단어의 경우 최대 빈도수를 maxv에 더하고 최소 빈도를 minv에 더하여 자카드 유사도를 구하였다. 아래는 내가 해결한 코드이다.
from collections import Counter
def solution(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
set1 = []
set2 = []
for i in range(len(str1) - 1):
if 96 < ord(str1[i]) < 123 and 96 < ord(str1[i + 1]) < 123:
set1.append(str1[i:i + 2])
for j in range(len(str2) - 1):
if 96 < ord(str2[j]) < 123 and 96 < ord(str2[j + 1]) < 123:
set2.append(str2[j:j + 2])
c1 = Counter(set1)
c2 = Counter(set2)
ming = set(set1).intersection(set(set2))
maxg = set(set1).union(set(set2))
minv = 0
maxv = 0
for word in maxg:
# str1에 없을 때
if c1.get(word) is None:
maxv += c2.get(word)
elif c2.get(word) is None:
maxv += c1.get(word)
else:
if c1.get(word) > c2.get(word):
minv += c2.get(word)
maxv += c1.get(word)
else:
minv += c1.get(word)
maxv += c2.get(word)
if maxv == 0:
return 65536
else:
return int(minv / maxv * 65536)
'알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
2018 KAKAO BLINDRECRUITMENT 1차 비밀지도 (0) | 2020.03.26 |
---|---|
2018 KAKAO BLINDRECRUITMENT 1차 캐시 (0) | 2020.03.26 |
2018 KAKAO BLINDRECRUITMENT 1차 프렌즈4블록 (0) | 2020.03.26 |
2018 KAKAO BLINDRECRUITMENT 1차 셔틀버스 (0) | 2020.03.26 |
2018 KAKAO BLINDRECRUITMENT 1차 추석 트래픽 (0) | 2020.03.26 |
Comments