Output each combination of an array of numbers with javascript

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I have several numbers in an array

var numArr = [1, 3, 5, 9]; 

I want to cycle through that array and multiply every unique 3 number combination as follows:

1 * 3 * 5 =  1 * 3 * 9 =  1 * 5 * 9 =  3 * 5 * 9 = 

Then return an array of all the calculations

var ansArr = [15,27,45,135]; 

Anyone have an elegant solution? Thanks in advance.

回答1:

A general-purpose algorithm for generating combinations is as follows:

function combinations(numArr, choose, callback) {     var n = numArr.length;     var c = [];     var inner = function(start, choose_) {         if (choose_ == 0) {             callback(c);         } else {             for (var i = start; i 

In your case, you might call it like so:

function product(arr) {     p = 1;     for (var i in arr) {         p *= arr[i];     }     return p; }  var ansArr = [];  combinations(     [1, 3, 5, 7, 9, 11], 3,     function output(arr) {         ansArr.push(product(arr));     });  document.write(ansArr); 

...which, for the given input, yields this:

15,21,27,33,35,45,55,63,77,99,105,135,165,189,231,297,315,385,495,693 


回答2:

I think this should work:

var a = [1, 3, 5, 9]; var l = a.length; var r = []; for (var i = 0; i 

Edit

Just for my own edification, I figured out a generic solution that uses loops instead of recursion. It's obvious downside is that it's longer thus slower to load or to read. On the other hand (at least on Firefox on my machine) it runs about twice as fast as the recursive version. However, I'd only recommend it if you're finding combinations for large sets, or finding combinations many times on the same page. Anyway, in case anybody's interested, here's what I came up with.

function combos(superset, size) {   var result = [];   if (superset.length  -1 ; --i) {         distance_back = indexes_last - i;         new_last_index = indexes[indexes_last - distance_back] + distance_back + 1;         if (new_last_index 


回答3:

A recursive function to do this when you need to select k numbers among n numbers. Have not tested. Find if there is any bug and rectify it :-)

var result = [];  foo(arr, 0, 1, k, n); // initial call  function foo(arr, s, mul, k, n) {     if (k == 1) {         result.push(mul);         return;     }      var i;     for (i=s; i

This is a recursive function.

  1. First parameter is array arr.

  2. Second parameter is integer s. Each call calculates values for part of the array starting from index s. Recursively I am increasing s and so array for each call is recursively becoming smaller.

  3. Third parameter is the value that is being calculated recursively and is being passed in the recursive call. When k becomes 1, it gets added in the result array.

  4. k in the size of combination desired. It decreases recursively and when becomes 1, output appended in result array.

  5. n is size of array arr. Actually n = arr.length



回答4:

var create3Combi = function(array) {     var result = [];     array.map(function(item1, index1) {         array.map(function(item2, index2) {             for (var i = index2 + 1; i 


回答5:

Using node, you can do this pretty easily using a library. First install bit-twiddle using npm:

npm install bit-twiddle 

Then you can use it in your code like this:

//Assume n is the size of the set and k is the size of the combination var nextCombination = require("bit-twiddle").nextCombination for(var x=(1

The variable x is a bit-vector where bit i is set if the ith element is contained in the combination.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!