1

If I have one array that contains items , for example

["A", "B", "C"]

I want to make a function that return an array with all possible permutations / all possible mixing results with all size from 1 to the number of items in array

For example result should be here:

["","A" "B", "C","AB", "BA", "AC", "CA", "CB","BC", "ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]

Could you help me please?

Pipo
  • 5,170
  • 7
  • 33
  • 66
  • What did you try? We're not supposed to develop stuff for you. You need to try something first and ask for help when your code doesn't work. – Jeremy Thille Mar 22 '15 at 16:35
  • Here's the really simple, not very elegant solution you should be able to figure out yourself without too much hazzle -> **http://jsfiddle.net/her8z7rs/** – adeneo Mar 22 '15 at 16:37
  • @ Jeremy Thille : you're right, but maybe permutation it is a classic algorithme who has a name and somebody with give me a link i did not found or give me main points cause would be useless to recode wheel. If people ask just when it is very very hard, this site woule be really less useful – Pipo Mar 22 '15 at 16:41
  • very good answere is here : https://stackoverflow.com/questions/29195881/javascript-jquery-how-to-get-permutations-from-items-of-an-array – Pipo Mar 22 '15 at 17:05
  • @VivienPipo: Please edit your questions instead of re-posting them. – Bergi Mar 22 '15 at 17:40
  • yes sorry, i did not know that i will have a answere to the both questions in my first post – Pipo Mar 22 '15 at 18:00

2 Answers2

2

I'm not that good at js,but I'll try to show you how to solve the problem and will provide you some code in c++ that is very similar to js.

At first, you must know the number of elements in the array

this is a js code here.

var arr=["a","b","c"];     
var l=arr.length;

Now here is the main idea:

The number of the all possible permutations, without the repeated elements is 2^l, l is the number of elements.

For arr above, the number of the all possible permutations is 2^3=8, which also equals (111) in binary +1, the +1 is for the empty set.

If you count from 0 to 7 you will have 8 elements. it's the same if you counted from (000) to (111) in binary.

The result of counting is:

000
001
010
011
100
101
110
111

and if you looked for the zeros and ones(you might say true or false), they would present the number of the all possible permutations without the repetition.(000:choose no element,001:choose the last element,..., 101 choose the first and last element,...,111:choose all elements). So now you could make a function that would generate the set {"","a","b","c","ab","ac","bc","abc"}.

Now you will need to find the possible mixes for the elements with more than one element. I don't know what is that in js, but in c++ I usually use next_permutation.

When you find the binary number, the number of the digits must be as the same as the number of the elements of the array.

//the code of converting the decimal number to binary.
strint tobinary(int l,int n){
    int x;
    string str="";
    //the while statement finds the binary equivalent
    while(n!=0){
    x=n/2;
    n=n%2;
    str=(x+'0')+str; //the '0' is equal to 48(the ASCII code of 0)
    }
    //the while statement adds zeros to the left of the binary number until it's length is equal to the number of the elements.
    while(str.length()!=l)
        str="0"+str;
    return str;
}

you must now define a vector or a dynamic array, and for 2^l times find the elements that are the opposites of the ones in the string str.

IF you want to find something like permutations in javascript, see here.

I hope my answer will help you;

Community
  • 1
  • 1
Abozanona
  • 2,261
  • 1
  • 24
  • 60
1

permute__of_all_size is the function made by @M Oehm that works well

   function swap(array, i, j) {
    if (i != j) {
        var swap = array[i];
        array[i] = array[j];
        array[j] = swap;
    }
}

function permute_rec(res, str, array) {
    if (array.length == 0) {
        res.push(str);
    } else {
        for (var i = 0; i < array.length; i++) {
            swap(array, 0, i);
            permute_rec(res, str + array[0], array.slice(1));
            swap(array, 0, i);
        }
    }
}

function permute(array) {
    var res = [];

    permute_rec(res, "", array);
    return res;
}

function xpermute_rec(res, sub, array) {
    if (array.length == 0) {
        if (sub.length > 0) permute_rec(res, "", sub);
    } else {
        xpermute_rec(res, sub, array.slice(1));
        xpermute_rec(res, sub.concat(array[0]), array.slice(1));
    }
}

function permute__of_all_size(array) {
    var res = [];

    xpermute_rec(res, [], array);
    return res;
}
Pipo
  • 5,170
  • 7
  • 33
  • 66