forked from 2021-Algorithm/algorithms_site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
news_clustering.py
103 lines (86 loc) · 2.65 KB
/
news_clustering.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from collections import Counter
def solution(str1, str2):
str1_list = []
str2_list = []
for i in range(len(str1)-1):
s = str1[i:i+2]
if s.isalpha():
str1_list.append(s.lower())
for i in range(len(str2)-1):
s = str2[i:i+2]
if s.isalpha():
str2_list.append(s.lower())
total = len(str1_list) + len(str2_list)
bunza = 0
if total == 0 :
return 65536
str1_list.sort()
str2_list.sort()
s2idx = 0
for i in range(len(str1_list)):
s = str1_list[i]
for j in range(s2idx, len(str2_list)):
if s == str2_list[j]:
s2idx = j+1
bunza += 1
total -= 1
break
if s2idx == len(str2_list):
break
return int(bunza/total * 65536)
def solution_old(str1, str2):
st1 = []
for i in range(len(str1)):
if i == len(str1) - 1 :
break
c1 = str1[i]
c2 = str1[i+1]
if c1.isalpha() and c2.isalpha() :
if c1.isupper():
c1 = c1.lower()
if c2.isupper():
c2 = c2.lower()
st1.append(c1+c2)
st2 = []
for i in range(len(str2)):
if i == len(str2) - 1 :
break
c1 = str2[i]
c2 = str2[i+1]
if c1.isalpha() and c2.isalpha() :
if c1.isupper():
c1 = c1.lower()
if c2.isupper():
c2 = c2.lower()
st2.append(c1+c2)
st1_counter = Counter(st1)
st2_counter = Counter(st2)
bunza = 0
bunmo = 0
for c1 in st1_counter:
if c1 in st2_counter:
bunza += min(st1_counter[c1], st2_counter[c1])
bunmo += max(st1_counter[c1], st2_counter[c1])
else:
bunmo += st1_counter[c1]
for c2 in st2_counter:
if not c2 in st1_counter:
bunmo += st2_counter[c2]
if bunmo == 0:
return 65536
return int(bunza / bunmo * 65536)
print(solution("FRANCE", "french"))
print(solution("handshake", "shake hands"))
print(solution("aa1+aa2", "AAAA12"))
print(solution("E=M*C^2", "e=m*c^2"))
print("aA".isalpha()) ##True
def solution2(str1, str2):
str1 = [str1[n:n+2].lower() for n in range(len(str1)-1) if str1[n:n+2].isalpha()]
str2 = [str2[n:n+2].lower() for n in range(len(str2)-1) if str2[n:n+2].isalpha()]
gyo = set(str1) & set(str2)
hap = set(str1) | set(str2)
if len(hap) == 0 :
return 65536
gyo_sum = sum([min(str1.count(gg), str2.count(gg)) for gg in gyo])
hap_sum = sum([max(str1.count(hh), str2.count(hh)) for hh in hap])
return int((gyo_sum/hap_sum)*65536)