diff --git a/src/problem1.js b/src/problem1.js index 9a14f2c..1dff2cf 100644 --- a/src/problem1.js +++ b/src/problem1.js @@ -1,6 +1,35 @@ function problem1(pobi, crong) { - var answer; - return answer; + if (!isValidPage(pobi) || !isValidPage(crong)) return -1; + + const pScore = Math.max(pageScore(pobi[0]), pageScore(pobi[1])); + const cScore = Math.max(pageScore(crong[0]), pageScore(crong[1])); + + if (pScore > cScore) return 1; + if (pScore < cScore) return 2; + return 0; +} + +function isValidPage(arr) { + if (!Array.isArray(arr) || arr.length !== 2) return false; + const [L, R] = arr; + if (!Number.isInteger(L) || !Number.isInteger(R)) return false; + + if (L < 1 || R > 400) return false; + if (L === 1 && R === 2) return false; + if (L === 399 && R === 400) return false; + + if (L % 2 !== 1) return false; + if (R % 2 !== 0) return false; + if (R !== L + 1) return false; + + return true; +} + +function pageScore(n) { + const digits = String(n).split('').map(Number); + const sum = digits.reduce((a, b) => a + b, 0); + const product = digits.reduce((a, b) => a * b, 1); + return Math.max(sum, product); } module.exports = problem1; diff --git a/src/problem2.js b/src/problem2.js index cebd07c..b41a2f7 100644 --- a/src/problem2.js +++ b/src/problem2.js @@ -1,6 +1,21 @@ function problem2(cryptogram) { - var answer; - return answer; + let changed = true; + + while (changed) { + changed = false; + let result = ""; + for (let i = 0; i < cryptogram.length; i++) { + if (cryptogram[i] === cryptogram[i + 1]) { + changed = true; + i++; + } else { + result += cryptogram[i]; + } + } + cryptogram = result; + } + + return cryptogram; } -module.exports = problem2; +module.exports = problem2; \ No newline at end of file diff --git a/src/problem3.js b/src/problem3.js index 1baed28..62e565f 100644 --- a/src/problem3.js +++ b/src/problem3.js @@ -1,6 +1,16 @@ function problem3(number) { - var answer; + let answer = 0; + + for (let i = 1; i <= number; i++) { + const str = String(i); + for (let item of str) { + if (item === "3" || item === "6" || item === "9") { + answer++; + } + } + } + return answer; } -module.exports = problem3; +module.exports = problem3; \ No newline at end of file diff --git a/src/problem4.js b/src/problem4.js index ee1d3bd..556e89c 100644 --- a/src/problem4.js +++ b/src/problem4.js @@ -1,6 +1,17 @@ function problem4(word) { - var answer; - return answer; + let result = ''; + + for (let item of word) { + if (item >= 'A' && item <= 'Z') { + result += String.fromCharCode('Z'.charCodeAt(0) - (item.charCodeAt(0) - 'A'.charCodeAt(0))); + } else if (item >= 'a' && item <= 'z') { + result += String.fromCharCode('z'.charCodeAt(0) - (item.charCodeAt(0) - 'a'.charCodeAt(0))); + } else { + result += item; + } + } + + return result; } module.exports = problem4; diff --git a/src/problem5.js b/src/problem5.js index 9368e87..8ae649f 100644 --- a/src/problem5.js +++ b/src/problem5.js @@ -1,6 +1,14 @@ function problem5(money) { - var answer; - return answer; + const units = [50000, 10000, 5000, 1000, 500, 100, 50, 10, 1]; + const result = []; + + for (let unit of units) { + const count = Math.floor(money / unit); + result.push(count); + money %= unit; + } + + return result; } module.exports = problem5; diff --git a/src/problem6.js b/src/problem6.js index 3f842b5..22b1f0a 100644 --- a/src/problem6.js +++ b/src/problem6.js @@ -1,6 +1,25 @@ function problem6(forms) { - var answer; - return answer; + let badEmails = []; + + for (let i = 0; i < forms.length; i++) { + const [email1, nickname1] = forms[i]; + + for (let j = i + 1; j < forms.length; j++) { + const [email2, nickname2] = forms[j]; + + for (let k = 0; k < nickname1.length - 1; k++) { + const part = nickname1.substring(k, k + 2); + if (nickname2.includes(part)) { + if (!badEmails.includes(email1)) badEmails.push(email1); + if (!badEmails.includes(email2)) badEmails.push(email2); + break; + } + } + } + } + + badEmails.sort(); + return badEmails; } module.exports = problem6; diff --git a/src/problem7.js b/src/problem7.js index ee1bb9d..07c9c50 100644 --- a/src/problem7.js +++ b/src/problem7.js @@ -1,6 +1,70 @@ function problem7(user, friends, visitors) { - var answer; - return answer; + var graph = {}; + for (var i = 0; i < friends.length; i++) { + var a = friends[i][0]; + var b = friends[i][1]; + + if (!graph[a]) graph[a] = []; + if (!graph[b]) graph[b] = []; + graph[a].push(b); + graph[b].push(a); + } + + var userFriends = []; + if (graph[user]) { + for (var i = 0; i < graph[user].length; i++) { + userFriends.push(graph[user][i]); + } + } + var score = {}; + + for (var i = 0; i < userFriends.length; i++) { + var friend = userFriends[i]; + var fList = graph[friend] ? graph[friend] : []; + + for (var j = 0; j < fList.length; j++) { + var person = fList[j]; + if (person === user) continue; + var isUserFriend = false; + for (var k = 0; k < userFriends.length; k++) { + if (userFriends[k] === person) { + isUserFriend = true; + break; + } + } + if (isUserFriend) continue; + if (!score[person]) score[person] = 0; + score[person] += 10; + } + } + + for (var i = 0; i < visitors.length; i++) { + var v = visitors[i]; + if (v === user) continue; + var isUserFriend = false; + for (var k = 0; k < userFriends.length; k++) { + if (userFriends[k] === v) { + isUserFriend = true; + break; + } + } + if (isUserFriend) continue; + if (!score[v]) score[v] = 0; + score[v] += 1; + } + + var keys = []; + for (var key in score) { + if (score[key] > 0) keys.push(key); + } + keys.sort(function (a, b) { + if (score[b] !== score[a]) return score[b] - score[a]; + if (a < b) return -1; + if (a > b) return 1; + return 0; + }); + + return keys.slice(0, 5); } module.exports = problem7;