-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec.js
45 lines (38 loc) · 1.06 KB
/
rec.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
Subsets = function(){this.retarr = []}
Subsets.prototype.merge = function(subset){
//console.log(subset)
this.retarr.push(subset.join(''))
}
Subsets.prototype.subsetize = function(superset, subset, subsetsize, nextindex){
if(subsetsize == subset.length){
this.merge(subset)
} else {
for (j=nextindex;j<superset.length;j+=1) {
subset[subsetsize] = superset[j]
this.subsetize(superset, subset, subsetsize+1, nextindex+1)
}
}
}
Subsets.prototype.getSubsets = function(superset, subsetsize) {
if (superset.length < 1) {
return new Error("SupersetSizeInvalid")
} else if (subsetsize < 0) {
return new Error("NoNegativeSubset")
} else if (superset.length < subsetsize){
return new Error("SupersetSizeTooShort")
} else if (subsetsize < 2) {
return new Error("SubsetSizeTooShort")
} else {
for(i=0;i<superset.length;i+=1) {
subset = new Array(subsetsize)
this.subsetize(superset,subset, 0, i)
}
return this.retarr
}
}
//Subsets.prototype.retarr = []
arr = [1,2,3,4,5,6,7,8,9]
arr2 = [1]
s = new Subsets()
x = s.getSubsets(arr,4)
console.log(x)