-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleRunner.js
47 lines (42 loc) · 1.81 KB
/
sampleRunner.js
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
const PangramTopDownDag = require('./lib/pangram-top-down-tree');
const PangramHeuristicSearch = require('./lib/pangram-heuristic-search');
const utils = require('./lib/pangram-string-utilities');
/*
Entry function into finding pangrams by using a Dynamic Programming tactic, building
pangrams from the provded PangramGraph by building a tree.
First, find all the words that contain exactly 1 letter. The nodes built are the
nodes "1 letter away" from the solution node. Then build all the nodes "2 letters away",
meaning you build nodes using every 2 letter word, then each available 1 letter word to
join with all the nodes from the previous step... Then continue until you have built the
tree layer by layer where each layer are all the nodes "n letters away" from being a perfect
pangram. Using this tactic [should be] significantly faster than the heuristics based approach,
especially if the heuristics are complex to compute.
Supppose:
alhbet: BADRIG
words: [A, I, BIG, BAD, AD, RIG]
layers built:
''
A I;
AD, I->A, A->I
BIG, BAD, RIG, I->AD
A->BIG
AD->BIG, AD->RIG
BAD->RIG (ONLY SOLUTION)
*/
function findAllPangrams(words) {
// const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const alphabet = 'ABCDEF';
let reverseDictionary = utils.createCompactDictionary(words, alphabet);
let graph = new PangramHeuristicSearch(alphabet, words);
graph.search();
let cannonPangrams = graph.getKnownPangrams();
console.log("Cannonized Pangrams Found:")
console.log(cannonPangrams);
for(let cannon of cannonPangrams) {
for(let word of cannon.split(';')) {
console.log(` ${word}: ${reverseDictionary.get(word)}`);
}
}
}
utils.loadDict(process.cwd() + '/scrabble-dictionary.csv', findAllPangrams);
// findAllPangrams().catch((e) => { throw e; });