Skip to content

Commit d41a9f9

Browse files
committed
Add combination computation and merge functions
Both functions manipulate arrays, the first one computes the permutations of various lengths, the second one merges the second array into the missing values in the first one.
1 parent e183fc1 commit d41a9f9

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

solver.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,47 @@ function partition_split_idx (A, B) {
202202
return null;
203203
}
204204

205+
function combinations (n, kmin, kmax) {
206+
if (kmax < 0 || n < kmin) {
207+
return [];
208+
}
209+
210+
if (n === 0) {
211+
return [[]];
212+
}
213+
214+
if (n === 1) {
215+
var res = [];
216+
if (kmax > 0) {
217+
res.push([1]);
218+
}
219+
if (kmin <= 0) {
220+
res.push([0]);
221+
}
222+
console.log(res);
223+
return res;
224+
}
225+
226+
var combsPlus = combinations (n-1, kmin-1, kmax-1);
227+
var combsMin = combinations (n-1, kmin, kmax);
228+
229+
combsPlus.map(function (el) { el.splice(0, 0, 1); return el; });
230+
combsMin.map( function (el) { el.splice(0, 0, 0); return el; });
231+
return combsPlus.concat(combsMin);
232+
}
233+
234+
function merge (arr1, arr2) {
235+
var s = arr1.slice();
236+
var lp = 0;
237+
for (var i = 0; i < arr2.length; ++i) {
238+
while (lp in s) {
239+
++lp;
240+
}
241+
s[lp] = arr2[i];
242+
}
243+
return s;
244+
}
245+
205246
/* Find all possible solutions for A * X = B assuming that both a_ij and x_i
206247
* are in {true, false}.
207248
*

0 commit comments

Comments
 (0)