Skip to content

Instantly share code, notes, and snippets.

@diunko
Created June 28, 2014 13:30
Show Gist options
  • Save diunko/a49cf81ec80ca9200957 to your computer and use it in GitHub Desktop.
Save diunko/a49cf81ec80ca9200957 to your computer and use it in GitHub Desktop.
diunko@boat4:~$ cat test.js
var cifri0 = [[1,2]];
var cifridva = [[1,2], [3,4]];
var cifri = [[1,2], [3,4], [5,6],[7,8]];
var cifri2 = [[1,2], [3,4], [5,6], [7,8],[9,10]];
function variate(arr) {
console.log("variate called with:",arr)
if (arr.length === 0) return "Incorrect Input";
if (arr.length === 1) return [[arr[0][0]],[arr[0][1]]]; //вот он !!! Базис рекурсивной функции :):):):)
var output = [];
var m = variate(arr.slice(0, arr.length-1));
for (var i = 0; i < m.length; i++){
var t = [arr[0][0]].concat(m[i]);
output.push(t);
};
for (var i = 0; i < m.length; i++) {
var k = [arr[0][1]].concat(m[i]);
output.push(k);
}
console.log("variate returns: ");
console.log(output);
return output;
};
var r = variate(cifri2)
console.log("===============");
console.log(r);
diunko@boat4:~$ node test.js
variate called with: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ]
variate called with: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
variate called with: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
variate called with: [ [ 1, 2 ], [ 3, 4 ] ]
variate called with: [ [ 1, 2 ] ]
variate returns:
[ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
variate returns:
[ [ 1, 1, 1 ],
[ 1, 1, 2 ],
[ 1, 2, 1 ],
[ 1, 2, 2 ],
[ 2, 1, 1 ],
[ 2, 1, 2 ],
[ 2, 2, 1 ],
[ 2, 2, 2 ] ]
variate returns:
[ [ 1, 1, 1, 1 ],
[ 1, 1, 1, 2 ],
[ 1, 1, 2, 1 ],
[ 1, 1, 2, 2 ],
[ 1, 2, 1, 1 ],
[ 1, 2, 1, 2 ],
[ 1, 2,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