JavaScript algorithm to merge two arrays into an array of all possible combinations algorithm combine arrays,merge algorithm array,algorithm merge sorted arrays,

Example given in JavaScript:

Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example:

mergeEveryWayPossible([0,0,0],[1,1,1])
// [ [0,0,0],[1,0,0], [0,1,0], [0,0,1], [1,1,0], [0,1,1], [1,0,1], [1,1,1] ]

merge the arrays into an array of all possible combinations. This is different than finding the cartesian product.

I'm also not sure what this kind of combination is called. If the algorithm or technique has a name, please share.

Answer:1

List monad

Whoever wrote that long thing about delimited whatchamacallits is nuts – after the 3 hours I spend trying to figure it out, I'll forget everything about it in 30 seconds !

On a more serious note, when compared to this answer, the shift/reset is so unbelievably impractical, it's a joke. But, if I didn't share that answer first, we wouldn't have had the joy of turning our brains inside out ! So please, don't reach for shift/reset unless they're critical to the task at hand – and please forgive me if you feel cheated into learning something totally cool !

Let's not overlook a more straightforward solution, the List monad – lovingly implemented with Array.prototype.chain here – also, notice the structural similarities between this solution and the continuation solution.

// monads do not have to be intimidating
// here's one in 2 lines†
Array.prototype.chain = function chain (f)
  {
    return this.reduce ((acc, x) =>
      acc.concat (f (x)), [])
  };

const permute = (n, choices) =>
  {
    const loop = (acc, n) =>
      n === 0
        ? [acc]
        : choices.chain (choice =>
            loop (acc.concat ([choice]), n - 1))
    return loop ([], n)
  }

console.log (permute (3, [0,1]))
// [ [ 0, 0, 0 ],
//   [ 0, 0, 1 ],
//   [ 0, 1, 0 ],
//   [ 0, 1, 1 ],
//   [ 1, 0, 0 ],
//   [ 1, 0, 1 ],
//   [ 1, 1, 0 ],
//   [ 1, 1, 1 ] ]

const zippermute = (xs, ys) =>
  {
    const loop = (acc, [x,...xs], [y,...ys]) =>
      x === undefined || y === undefined
        ? [acc]
        : [x,y].chain (choice =>
            loop (acc.concat ([choice]), xs, ys))
    return loop ([], xs, ys)
  }

console.log (zippermute (['a', 'b', 'c'], ['x', 'y', 'z']))
// [ [ 'a', 'b', 'c' ],
//   [ 'a', 'b', 'z' ],
//   [ 'a', 'y', 'c' ],
//   [ 'a', 'y', 'z' ],
//   [ 'x', 'b', 'c' ],
//   [ 'x', 'b', 'z' ],
//   [ 'x', 'y', 'c' ],
//   [ 'x', 'y', 'z' ] ]
Answer:2

You can use lodash, here's their implementation:

(function(_) {

    _.mixin({combinations: function(values, n) {
        values = _.values(values);
        var combinations = [];
        (function f(combination, index) {
            if (combination.length < n) {
                _.find(values, function(value, index) {
                    f(_.concat(combination, [value]), index + 1);
                }, index);
            } else {
                combinations.push(combination);
            }
        })([], 0);
        return combinations;
    }});

})((function() {
    if (typeof module !== 'undefined' && typeof exports !== 'undefined' && this === exports) {
        return require('lodash');
    } else {
        return _;
    }
}).call(this));

console.log(_.combinations('111000', 3))
console.log(_.combinations('111000', 3).length + " combinations available");

This would log out the following:

[["1", "1", "1"], ["1", "1", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "1", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "0", "0"], ["1", "0", "0"], ["0", "0", "0"]]

"20 combinations available"

There library is at https://github.com/SeregPie/lodash.combinations

Answer:3

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

  1. trigger dataflow from cloud function
  2. send email from cloud function
  3. query firestore from cloud function
  4. trigger dataflow from cloud function python
  5. run dataflow from cloud function
  6. get response from cloud function
  7. write to firestore from cloud function
  8. firebase send notification from cloud function
  9. call cloud function from cloud function

Currently screensharing on localhost in firefox throws the following error: The request is not allowed by the user agent or the platform in the current context.. Here's my code: navigator....

Currently screensharing on localhost in firefox throws the following error: The request is not allowed by the user agent or the platform in the current context.. Here's my code: navigator....

Trying to migrate from react 15.6.1 to 16.0.0. I have updated my package.json, to "react": "16.0.0","react-dom": "16.0.0", (only things I changed so far), and from what I've understood from the ...

Trying to migrate from react 15.6.1 to 16.0.0. I have updated my package.json, to "react": "16.0.0","react-dom": "16.0.0", (only things I changed so far), and from what I've understood from the ...

Here is my initial code: Where I can add textbox by clicking the add button. <script type="text/javascript"> function addRow(tableID) { var table = document.getElementById(tableID); ...

Here is my initial code: Where I can add textbox by clicking the add button. <script type="text/javascript"> function addRow(tableID) { var table = document.getElementById(tableID); ...

  1. total value of sales crossword clue
  2. total value of assets
  3. total value of us stock market
  4. total value of ownership
  5. total value of a number
  6. total value of bitcoin
  7. total value in zerodha
  8. total value of sales
  9. total value of amazon
  10. total value of apple
  11. total value of stock market
  12. total value of tata group
  13. total value of pi
  14. total value in maths
  15. total value of s&p 500
  16. total value of indian economy
  17. total value of india
  18. total value of sales crossword
  19. total value of us economy
  20. total value of world economy