File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ def solution (str1 , str2 ):
2+ """
3+ 1. 다중집합 만들기
4+ - 문자열을 2글자씩 끊기
5+ - 두 글자가 모두 알파벳일 때에만 저장하기 (`isalpha()` 이용)
6+ - 대소문자 구분이 없으므로 모두 대문자 또는 소문자 둘 중 하나로 통일하기 (`upper()` or `lower()` 이용)
7+ 2. 다중집합 간의 교집합과 합집합 구하기
8+ - 다중집합은 중복된 원소를 포함할 수 있다. 따라서 `remove()`를 이용해서 하나씩 비교하며 처리한다.
9+ - 교집합: A 집합과 B 집합의 공통된 원소를 제거해가면서 교집합을 저장할 리스트(`intersect`)에 저장한다
10+ - 합집합: A의 차집합(A - B) + B의 차집합(B - A) + 교집합으로 구성한다.
11+ 3. 자카드 유사도 구하기
12+ - 합집합이 공집합일 경우(=합집합 리스트 길이가 0), 유사도는 1로 처리한다.
13+ - 그게 아닐 경우, `교집합 / 합집합`
14+ 4. 정답 반환
15+ - `자카드 유사도 * 65536` 의 정수 부분을 반환한다.
16+ """
17+ # 1. 다중집합 만들기
18+ a = []
19+ for i in range (len (str1 ) - 1 ):
20+ if str1 [i :i + 2 ].isalpha ():
21+ a .append (str1 [i :i + 2 ].upper ())
22+
23+ b = []
24+ for i in range (len (str2 ) - 1 ):
25+ if str2 [i :i + 2 ].isalpha ():
26+ b .append (str2 [i :i + 2 ].upper ())
27+
28+ # 2.1. 다중합집합 구하기
29+ union_set = a .copy ()
30+ a_minus_b = a .copy () # A의 차집합
31+
32+ for i in b :
33+ if i not in a_minus_b : # b에는 있지만 a에 있는 건 b의 차집합이다. 따라서 Union에 추가한다.
34+ union .append (i )
35+ else : # a_minus_b는 a의 차집합으로 b와 중복되는 게 있다면 지워야 한다.
36+ a_minus_b .remove (i )
37+
38+ # 2.2. 다중교집합 구하기
39+ inter_set = []
40+ for i in b :
41+ if i in a :
42+ a .remove (i )
43+ inter_set .append (i )
44+
45+ # 3. 자카드 유사도 계산
46+ if len (union ) == 0 : # 합집합이 0이라면
47+ return 65536
48+
49+ similarity = len (inter_set ) / len (union )
50+
51+ # 정답 출력
52+ return int (similarity * 65536 )
You can’t perform that action at this time.
0 commit comments