-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (66 loc) · 1.96 KB
/
server.js
File metadata and controls
74 lines (66 loc) · 1.96 KB
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
"use strict";
const fs = require('fs');
class Programm {
constructor() {
this.fileName = (process.argv)[2];
this.lookupTree = {};
this.result = [];
this.visited = [];
this.readFile();
}
readFile() {
if (!this.fileName) { throw Error('please add a filename program argument'); }
const splitFile = fs.readFileSync(this.fileName, 'utf-8').split('\n');
const firstLetters = splitFile.map(item => item.slice(0,1));
this.dictionary = splitFile
.filter(item => item !== '');
}
buildLookupTree() {
for (let io = 0; io < this.dictionary.length; io++) {
const word = this.dictionary[io];
const lastCharacter = word.slice(-1);
for (let ii = 0; ii < this.dictionary.length; ii++) {
let childWord = this.dictionary[ii];
if (lastCharacter === childWord.slice(0, 1) && word !== childWord) {
(this.lookupTree[io] || (this.lookupTree[io] = [])).push(ii);
}
}
}
this.lookupTree[this.dictionary.length] = Array.from(this.dictionary.keys());
}
isVisited(index) {
return this.visited[index];
}
visit(index) {
this.visited[index] = true;
}
exit(index) {
this.visited[index] = false;
}
find(currentIndex, rest, depth) {
let list = this.lookupTree[currentIndex];
if (!this.lookupTree[currentIndex]) {
return rest;
}
for (let i = 0; i < list.length; i++) {
let nextIndex = list[i];
if (!this.isVisited(nextIndex)) {
this.visit(nextIndex);
rest[depth] = nextIndex;
let candidate = this.find(nextIndex, rest, depth + 1);
this.exit(nextIndex);
if (depth + 1 > this.result.length) {
this.result = candidate.slice(0, depth + 1);
}
}
}
return rest;
}
processFile() {
this.buildLookupTree();
this.find(this.dictionary.length, [], 0);
console.log(this.result.map(i => this.dictionary[i]));
}
}
const pokemonFinder = new Programm();
pokemonFinder.processFile();