Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/data-sturcture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class DefaultMap extends Map {
constructor(defaultFn, entries) {
super(entries);
this.default = defaultFn;
}

get(key) {
if (!this.has(key)) this.set(key, this.default());

return super.get(key);
}

pop(key) {
const ret = this.get(key);
super.delete(key);

return ret;
}
}

module.exports = {
DefaultMap: DefaultMap
};
22 changes: 20 additions & 2 deletions src/problem1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
function problem1(pobi, crong) {
var answer;
return answer;

[p, c] = [pobi, crong].map(pages => {
if (pages[0] + 1 != pages[1]) return -1;

return Math.max(
...pages.map(page => {
var nums = page.toString().split("").map(Number);

if (nums.includes(0)) {
return nums.reduce((sum, cur) => sum + cur, 0);
}

return nums.reduce((mul, cur) => mul * cur, 1);
})
);
});

if ([p, c].includes(-1)) return -1;

return p == c ? 0 : (p < c ? 2 : 1);
}

module.exports = problem1;
8 changes: 7 additions & 1 deletion src/problem2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function problem2(cryptogram) {
var answer;
let answer;

while (cryptogram != answer) {
answer = cryptogram;
cryptogram = cryptogram.replace(/(.+)\1+/g, '');
}

return answer;
}

Expand Down
7 changes: 6 additions & 1 deletion src/problem3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function problem3(number) {
var answer;
const answer = [...Array(number).keys()]
.reduce((sum, cur) => {
return (cur + 1).toString().split("")
.filter(num => "369".includes(num)).length + sum;
}, 0);

return answer;
}

Expand Down
8 changes: 7 additions & 1 deletion src/problem4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function problem4(word) {
var answer;
const answer = word.replace(/[a-zA-Z]/g, char =>
String.fromCharCode(
char.toLowerCase() == char
? 219 - char.charCodeAt()
: 155 - char.charCodeAt()
)
);
return answer;
}

Expand Down
7 changes: 6 additions & 1 deletion src/problem5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function problem5(money) {
var answer;
const answer = [50000, 10000, 5000, 1000, 500, 100, 50, 10, 1]
.map(div => {
[ret, money] = [Math.floor(money / div), money % div];
return ret;
}
);
return answer;
}

Expand Down
27 changes: 26 additions & 1 deletion src/problem6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// LCS(Longest Common Substring) Algolrithm
const lcs = (a, b) => {
const dp = Array(a.length + 1).fill().map(() => Array(b.length + 1).fill(0));

for (let i = 1; i <= a.length; i++) {
for (let j = 1; j <= b.length; j++) {
dp[i][j] = a[i - 1] === b[j - 1] ? dp[i - 1][j - 1] + 1 : 0;
if (dp[i][j] > 1) return true;
}
}

return false;
};

function problem6(forms) {
var answer;
const answer = [
...forms.reduce((dup, [email1, nick1], i) => {
forms.slice(i + 1).forEach(([email2, nick2]) => {
if (lcs(nick1, nick2))
dup.add(email1).add(email2);
});

return dup;
},
new Set())
].sort();

return answer;
}

Expand Down
30 changes: 28 additions & 2 deletions src/problem7.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
const { DefaultMap } = require('./data-sturcture');

function problem7(user, friends, visitors) {
var answer;
return answer;
const graph = new DefaultMap(() => []);

friends.forEach(([u, v]) => {
graph.get(u).push(v);
graph.get(v).push(u);
});

const answer = [...graph.keys()].reduce((scores, key) => {
if (key === user || graph.get(user).includes(key)) {
return scores.set(key, undefined);
}

const score = graph.get(key).reduce((acc, val) => {
return acc + graph.get(user).includes(val);
}, 0) * 10;

return scores.set(key, score);
}, new DefaultMap(() => 0));

visitors.forEach(key => answer.set(key, answer.get(key) + 1));

return [...answer]
.filter(([_, score]) => !isNaN(score))
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(a => a[0]);
}

module.exports = problem7;