1

I have an array with some chars in it:


    var ar1 = ["a", "1"];

I want to create arrays with all permutations of this array, BUT which would have had given amount of slots like so:

if amount of slots is 3 the arrays would look like this:

    ["a", "1", "a"]
    ["1", "a", "1"]
    ["a", "a", "a"]
    ["1", "1", "1"]
    ["a", "a", "1"]
    ["1", "1", "a"]
    ["a", "1", "1"]
    ["1", "a", "a"]

and so on...

how can I make this happen?

  • 1
    Does this answer your question? [JavaScript - Generating combinations from n arrays with m elements](https://stackoverflow.com/questions/15298912/javascript-generating-combinations-from-n-arrays-with-m-elements) – Heretic Monkey Dec 15 '20 at 21:59

3 Answers3

0

I would solve this using recursion, something like this:

function permutations(possibleCharacters, n){
    const result = [];

    if(n == 0){
        return [[]];
    }

    const subProblemResult = permutations(possibleCharacters, n - 1);

    for(let character of possibleCharacters){
        for(let sub of subProblemResult){
            result.push([character].concat(sub));
        }
    }

    return result;
}

console.log(permutations(["a", "1"], 3));
Mulan
  • 129,518
  • 31
  • 228
  • 259
Syder
  • 372
  • 3
  • 9
0

Problem: Finding all possible combination of an input given the values and the length of each combination. inp = Input(Array), Len = Length of each combination.

function permutation(inp, len) {
   let posComb = 1,  r = 1;
   for ( let i =1; i<n/2+1; i++ )  {
      posComb = posComb * (inp-i+1)
      r = r * i;
      if ( posComb / r == inp ) return i;      
   }
   return -1;
} 

Explanation: This finds all possible combination of a and input and a desired length. Now you just have to create an array and fill it with the permutation, now you know exactly how many.

Credit to dcieslak from CodeWar for Code.

JimiA
  • 91
  • 4
-1

I suggest abusing toString() method:

var ar1 = ["a", "1"]

let n = 3
let base = ar1.length

let output = []

for(let i = 0; i < base**n; ++i) {
  output.push(i
    .toString(base)
    .padStart(n, '0')
    .split('')
    .map(x => ar1[parseInt(x, base)])
  )
}

console.log(output)

This way you can avoid recursion.

punund
  • 4,321
  • 3
  • 34
  • 45
  • what does `digits` have to do with this? why are you doing base conversion? might be you copied/pasted code from somewhere else? – Mulan Dec 15 '20 at 22:07
  • I just wrote this code myself. If you read it you wouldn't have to ask rhetorical questions. – punund Dec 15 '20 at 22:08
  • these aren't rhetorical questions. i did read it and noticed `digits` is unused and `padStart` doesn't impact this particular program. these are obvious signs that this code has been copied/pasted from elsewhere. – Mulan Dec 15 '20 at 22:14
  • `digits` was a leftover from previous version. `padStart` does affect this program, without it leading zeroes would have been gone and arrays would have been of uneven length. As for copying from elsewhere... I am `punund` on github, you can check my js code there. Look for the distinctive style (no semicolons). – punund Dec 15 '20 at 22:17
  • i didn't accuse you of not writing it. i copy/paste my own programs all the time. – Mulan Dec 15 '20 at 22:18
  • Even if it were the case (which it wasn't), why would it disqualify the answer? I was moderating first posts and saw this question, unanswered for some time, and made an answer which woudn't use recursion, which is not always easy to follow. If this were a copy-pasted code, I must be very lucky to have the same `ar1` variable as the OP. – punund Dec 15 '20 at 22:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226007/discussion-between-punund-and-thank-you). – punund Dec 15 '20 at 22:26