Lv2. 뉴스 클러스터링
https://programmers.co.kr/learn/courses/30/lessons/17677?language=javascript
function solution(prev, next) {
function verify(char) {
return (char.charCodeAt() >= 65 && char.charCodeAt() <= 90)
}
function convert(string) {
const upperString = string.toUpperCase()
const bindedString = []
for (let i = 0; i < upperString.length - 1; i++) {
if (verify(upperString[i]) && verify(upperString[i + 1])) {
bindedString.push(upperString[i] + upperString[i + 1])
}
}
return bindedString
}
const convertedPrev = convert(prev)
const convertedNext = convert(next)
if (!convertedPrev.length && !convertedNext.length) return 65536
const intersection = [] // 교집합
for (let i = 0; i < convertedPrev.length; i++) {
for (let j = 0; j < convertedNext.length; j++) {
if (convertedPrev[i] === convertedNext[j]) {
intersection.push(convertedNext[j])
convertedNext.splice(j, 1)
j--
break;
}
}
}
const union = convertedPrev.concat(convertedNext) // 합집합
return Math.floor(intersection.length / union.length * 65536)
}
function solution(prev, next) {
function verify(char) {
return (char.charCodeAt() >= 65 && char.charCodeAt() <= 90)
}
function convert(string) {
const upperString = string.toUpperCase()
const bindedString = []
for (let i = 0; i < upperString.length - 1; i++) {
if (verify(upperString[i]) && verify(upperString[i + 1])) {
bindedString.push(upperString[i] + upperString[i + 1])
}
}
return bindedString
}
const convertedPrev = convert(prev)
const convertedNext = convert(next)
if (!convertedPrev.length && !convertedNext.length) return 65536
const intersection = [] // 교집합
for (let i = 0; i < convertedPrev.length; i++) {
for (let j = 0; j < convertedNext.length; j++) {
if (convertedPrev[i] === convertedNext[j]) {
intersection.push(convertedNext[j])
convertedNext.splice(j, 1)
j--
break;
}
}
}
const union = convertedPrev.concat(convertedNext) // 합집합
return Math.floor(intersection.length / union.length * 65536)
}
function solution (str1, str2) {
function explode(text) {
const result = [];
for (let i = 0; i < text.length - 1; i++) {
const node = text.substr(i, 2);
if (node.match(/[A-Za-z]{2}/)) {
result.push(node.toLowerCase());
}
}
return result;
}
const arr1 = explode(str1);
const arr2 = explode(str2);
const set = new Set([...arr1, ...arr2]);
let union = 0;
let intersection = 0;
set.forEach(item => {
const has1 = arr1.filter(x => x === item).length;
const has2 = arr2.filter(x => x === item).length;
union += Math.max(has1, has2);
intersection += Math.min(has1, has2);
})
return union === 0 ? 65536 : Math.floor(intersection / union * 65536);
}
- 자카드 유사도를 설명하는 부분에서 숫자가 사용되어, 문제풀이에도 숫자를 포함하는 것으로 착각하고 한참을 헤맸던 문제.. 다 풀어놓고 시간을 오래 허비했다. 문제를 똑바로 읽자.
substr
,set
,match
(정규식)을 이용한 다른 사람 풀이를 보니까 약간의 현타;
댓글 환영
질문 환영
by.protect-me