-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble.js
92 lines (78 loc) · 2.1 KB
/
scrabble.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
87
88
89
90
91
92
/**
* Created by highlander on 3/17/17.
*/
var Redis = require('then-redis')
var redis = Redis.createClient('tcp://localhost:6379');
let voules = ["a","e","i","o","u"]
//
let letterbank = ["c","a","t"]
let fs = require('fs')
let dictionary
//7 letters
fs.readFile("./../data/dictionary.txt","utf8",function(err, read_data){
if(err) throw err
console.log(read_data)
read_data.split("\n")
//iterate over
for (var i = 0; i < read_data.length; i++) {
dictionary.push(read_data[i])
//get word
//Optimazation
// console.log("Word: ",read_data[i])
// let word = read_data[i]
// //strip non vouls
// let voulIndexA = []
// for (var j = 0; j < word.length; j++) {
// if(voules.indexOf(word[j]) >= 0) voulIndexA.push(word[j])
// }
// let voulIndex = voulIndexA.toString()
// redis.zadd(voulIndex,word.length(),word)
}
})
var permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
let findWords = function(words){
let permutations = permute(words)
let words = []
//find all permuations
for (i = 0; i < permutations.length; i++) {
if(dictionary.indexOf(permutations[i]) >= 0){
words.push(permutations[i])
}
}
//indexofDictionary
return words
}
vorpal.command('scabble [items...]', ' Params: letters')
//.option('-a')
.action( async(function (args,cb){
var tag = " | run | "
try{
let items = args.items[0]
//
let result = await(findWords(items))
console.log(result)
cb()
}catch(e){
console.error(tag," ERROR: Failed to get depist e:",e)
}
const self = this;
}))
//dictionary
//sort by vowl
//score by length
//words