3

I am trying to generate all possible combinations of a string.

e.g. for the list below: a1q5z!H9, b1q5z!H9, c1q5z!H9, d1q5z!H9, a2q5z!H9 ... etc

Rather than make lots of nested loops, I thought I would try something clever with MODULO ... but hit a wall.

This is the Javascript I have come up with - any pointers to how I might go on?

var c = [
  ['a', 'b', 'c', 'd'],
  ['1', '2', '3', '4'],
  ['q', 'w', 'e', 'r'],
  ['5', '6', '7', '8'],
  ['z', 'x', 'c', 'v'],
  ['!', '"', '£', '$'],
  ['H', 'J', 'K', 'L'],
  ['9', '8', '7', '6'],
];

var o = document.getElementById('output');
var pw = "";
var chars = c.length;

for( var i = 0; i <20; i++)
{
  pw = ""
  for(var j = 0; j < chars; j++ )
    {
      pw += c[j][i%4];
    }
  op(pw);
}

function op(s)
{
  o.innerHTML = o.innerHTML + "<br>" + s;
}

This just outputs the first 20 in the list, but repeats ... I nearly have it but not quite. Any help or pointers appreciated.

Ruskin
  • 5,721
  • 4
  • 45
  • 62

3 Answers3

6

Quite easy to write a recursive function demo.

function permutate(abc, memo) {
    var options;
    memo = memo || abc.shift().slice(0);

    if(abc.length) {
        options = abc.shift();

        return permutate(abc, memo.reduce(function(all, item){
            return all.concat(options.map(function(option){
                return item + option;
            }))
        }, []));       
    }

    return memo;
};

console.log(permutate(c).length); //65536 items

Or more imperative approach

function permutate2(abc) {
    var options, i, len, tmp, j, optionsLen, 
        memo = abc.pop().slice(0); //copy first the last array

    while(options = abc.pop()) { //replace recursion
        tmp = [];
        optionsLen = options.length;
        for(i = 0, len = memo.length; i < len; i++) { //for every element in memo
            for(j = 0; j < optionsLen; j++) { //do cartesian product with options
                tmp.push(options[j] + memo[i]);    
            }
        }
        memo = tmp;
    }

    return memo;
}
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • Returns the strings in the wrong order (cba rather than abc) but other than that - wow ... going to take me a while to work out how that works. Thank you – Ruskin Jun 24 '14 at 13:52
  • 1
    Play with `abc.shift` vs `abc.pop` and `option + item` vs `item + option` to get the order you need. – Yury Tarabanko Jun 24 '14 at 13:54
  • 1
    @Ruskin No problem :) If you have any questions about any specific line of code do not hesitate to ask. The code looks tricky indeed. – Yury Tarabanko Jun 24 '14 at 14:19
  • Understanding more of it via http://codepen.io/elliz/pen/vbeBq?editors=001 but still not entirely sure of how the map reduce is working - though can see what it does. – Ruskin Jun 24 '14 at 15:10
  • 1
    See my fully imperative example without fancy map|reduce :) http://jsfiddle.net/tarabyte/tKM2c/5/ – Yury Tarabanko Jun 24 '14 at 15:12
1
function combinations(str) {

debugger;
var arr = [];   
for (var i = 0; i < str.length; i++) 
{
    // create an empty string
    var comb = "";
    // loop for substring
    for (var j = i; j < str.length; j++) 
        {
        comb+=str[j];
        arr.push(comb);
        }
}
  return arr;

}
console.log(combinations('output'));
-1

This is how I did it:

string password;
bool done = false;

while (!done) {
    password = string.Empty;

    for(int a = 0; a < blah[0].Length; a++) {
        for(int b = 0; b < blah[1].Length; b++) {
            for (int c = 0; c < blah[2].Length; c++) {
                for (int d= 0; d < blah[3].Length; d++) {
                    for (int e = 0; e < blah[4].Length; e++) {
                        for (int f = 0; f < blah[5].Length; f++) {
                            for (int g = 0; g < blah[6].Length; g++) {
                                for (int h = 0; h < blah[7].Length; h++) {
                                    password = string.Format(
                                        "{0}{1}{2}{3}{4}{5}{6}{7}", 
                                        blah[0][a], 
                                        blah[1][b], 
                                        blah[2][c], 
                                        blah[3][d], 
                                        blah[4][e], 
                                        blah[5][f], 
                                        blah[6][g], 
                                        blah[7][h]);

                                    Console.Out.WriteLine(password);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Where blah is the character matrix.

Ugly, granted, but the shorter answer by Yury hurts my head.

Ruskin
  • 5,721
  • 4
  • 45
  • 62
wavydavy
  • 369
  • 3
  • 8