-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_wordfinder.js
97 lines (66 loc) · 2.11 KB
/
js_wordfinder.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
// main wordfinder function, called after Submit button
function InitialWords(){
var finalsolutions = [];
var letters = $('#letters').val(); // get entered letters
letters_or = getOrdered(letters); // arrange into alphabetical order
var checkset = getSubsets(letters_or.split('')); // find all subsets
// for each subset, find all words that can be formed with it
for (var i= 0; i < checkset.length; i++){
finalsolutions = FindWords(checkset[i], finalsolutions);
}
// output
if (finalsolutions.length == 0){
$('#wordsfound').text('No words found.');
} else {
$('#wordsfound').text('Solutions: '+finalsolutions.join(', '));
}
}
// Find all exact anagrams and put in finalsolutions
function FindWords(letters, finalsolutions){
var letters_or = getOrdered(letters);
if (letters_or in wordhash){ // check if ordered version has words
for (var i=0; i<wordhash[letters_or].length; i++){
finalsolutions.push(wordhash[letters_or][i]); // grab each mapped word
}
}
return finalsolutions;
}
// return word in alphabetical order
function getOrdered(letterlist){
var sorted = letterlist.split('').sort();
return sorted.join('');
}
// word is an array, returns all non-empty subsets
// all solutions returned are in alphabetical order
function getSubsets(word){
var solution = [];
if (word.length == 1){
return word;
} else {
// decided to use divide and conquer for subsets
var upper = Math.ceil(word.length / 2);
var bottom = word.slice(0, upper);
var top = word.slice(upper);
var lhs = getSubsets(bottom);
var rhs = getSubsets(top);
// take every LHS subset, and also combine with every RHS subset
for (var i = 0; i < lhs.length; i++){
solution.push(lhs[i]);
for (var j = 0; j < rhs.length; j++){
solution.push(lhs[i]+rhs[j]);
}
}
// don't forget RHS subsets on their own
for (var k = 0; k < rhs.length; k++){
solution.push(rhs[k]);
}
var solutiontemp = []
// eliminate non-unique subsets
for (i = 0; i < solution.length; i++){
if (solutiontemp.indexOf(solution[i]) < 0){
solutiontemp.push(solution[i])
}
}
return solutiontemp;
}
}