diff --git a/src/problem1.js b/src/problem1.js index 9a14f2c..6af7f9e 100644 --- a/src/problem1.js +++ b/src/problem1.js @@ -1,6 +1,41 @@ function problem1(pobi, crong) { var answer; + if ((pobi[0] + 1 !== pobi[1]) + || (crong[0] + 1 !== crong[1]) + || (pobi[0] < 1 || pobi[0] > 400) + || (crong[0] < 1 || crong[0] > 400)) { + return -1; + } + const pobimax = getMax(pobi); + const crongmax = getMax(crong); + if (pobimax > crongmax) answer = 1; + else if (pobimax < crongmax) answer = 2; + else answer = 0; return answer; } -module.exports = problem1; +function getMax(_arr) { + const arr=[]; + for (let _a of _arr) { + let a=_a; + const row=[]; + while (a > 0) { + row.push(a % 10); + a = Math.floor(a / 10); + } + arr.push(row); + } + const maxArr=[]; + for (let row of arr) { + let sum=0; + let mul=1; + for (let n of row) { + sum += n; + mul *= n; + } + maxArr.push(sum, mul); + } + return Math.max(...maxArr); +} + +module.exports = problem1; \ No newline at end of file diff --git a/src/problem2.js b/src/problem2.js index cebd07c..28daeaa 100644 --- a/src/problem2.js +++ b/src/problem2.js @@ -1,6 +1,16 @@ function problem2(cryptogram) { var answer; + let i=0; + answer = cryptogram; + while (i0) { + r=n%10; + if (r === 3 + || r === 6 + || r === 9 + ) { + answer++; + } + n=Math.floor(n/10); + } + } return answer; } diff --git a/src/problem4.js b/src/problem4.js index ee1d3bd..8321360 100644 --- a/src/problem4.js +++ b/src/problem4.js @@ -1,5 +1,18 @@ function problem4(word) { var answer; + answer = ""; + for ( c of word ) { + let a; + if ( c>="A" && c<="Z" ) { + a = String.fromCharCode( "Z".charCodeAt(0) - ( c.charCodeAt(0) - "A".charCodeAt(0) ) ); + answer+=a; + } else if ( c>="a" && c<="z" ) { + a = String.fromCharCode( "z".charCodeAt(0) - ( c.charCodeAt(0) - "a".charCodeAt(0) ) ); + answer+=a; + } else { + answer+=c; + } + } return answer; } diff --git a/src/problem5.js b/src/problem5.js index 9368e87..e27bc5e 100644 --- a/src/problem5.js +++ b/src/problem5.js @@ -1,5 +1,11 @@ function problem5(money) { var answer; + answer = []; + const coinTypes = [50000, 10000, 5000, 1000, 500, 100, 50, 10, 1]; + for (let c of coinTypes) { + answer.push(Math.floor(money / c)); + money = money % c; + } return answer; } diff --git a/src/problem6.js b/src/problem6.js index 3f842b5..ea38974 100644 --- a/src/problem6.js +++ b/src/problem6.js @@ -1,6 +1,29 @@ function problem6(forms) { var answer; - return answer; + answer = []; + for (let i = 0; i < forms.length; i++) { + for (let j = i + 1; j < forms.length; j++) { + if (checkSimilar(forms[i][1], forms[j][1])) { + if (!answer.includes(forms[i][0])) { + answer.push(forms[i][0]); + } + if (!answer.includes(forms[j][0])) { + answer.push(forms[j][0]); + } + } + } + } + return answer.sort(); +} +function checkSimilar(_name1, _name2) { + for (let i = 1; i < _name1.length; i++) { + for (let j = 1; j < _name2.length; j++) { + if ((_name1[i-1] === _name2[j-1]) + && (_name1[i] === _name2[j])) { + return true; + } + } + } + return false; } - module.exports = problem6; diff --git a/src/problem7.js b/src/problem7.js index ee1bb9d..779410a 100644 --- a/src/problem7.js +++ b/src/problem7.js @@ -1,5 +1,51 @@ function problem7(user, friends, visitors) { var answer; + let score = new Map(); + let userFriends = []; + for (let f of friends) { + if (f[1] === user) { + userFriends.push(f[0]); + } else if (f[0] === user) { + userFriends.push(f[1]); + } + } + for (let f of friends) { + if (userFriends.includes(f[0]) + && f[1] !== user + && !userFriends.includes(f[1])) { + if (score.has(f[1])) { + score.set(f[1], score.get(f[1]) + 10); + } else { + score.set(f[1], 10); + } + } else if (userFriends.includes(f[1]) + && f[0] !== user + && !userFriends.includes(f[0])) { + if (score.has(f[0])) { + score.set(f[0], score.get(f[0]) + 10); + } else { + score.set(f[0], 10); + } + } + } + for (let v of visitors) { + if (v !== user && !userFriends.includes(v)) { + if (score.has(v)) { + score.set(v, score.get(v) + 1); + } else { + score.set(v, 1); + } + } + } + arr = Array.from(score); + arr.sort((a, b) => (b[1] - a[1]) + || a[0].localeCompare(b[0])); + answer = []; + const n = arr.length > 5 ? 5 : arr.length; + for (let i = 0; i < n; i++) { + answer.push(arr[i][0]); + } + return answer; }