2014-06-24 58 views
3

我正在嘗試生成字符串的所有可能組合。生成字符串的所有可能組合

例如對於下面的列表:a1q5z!H9,b1q5z!H9,c1q5z!H9,d1q5z!H9,a2q5z!H9 ...等

而不是做很多嵌套循環,我想我會嘗試一些巧妙的MODULO。但是撞牆了。

這是我想出來的Javascript - 我可以繼續下去的任何指針?

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; 
} 

這只是輸出列表中的前20個,但重複...我幾乎擁有它,但不完全。任何幫助或指針讚賞。

+0

爲什麼'c'結構是這樣的? – Cerbrus

+0

我知道字符串連接並不是最有效的,但這是快速且髒的代碼 - 未優化。 – Ruskin

+0

快速和骯髒的字符陣列 - 首先想到的。數據是沒有意義的 - 只是隨機字符 – Ruskin

回答

6

很容易寫出遞歸函數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 

或者更迫切的方法

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; 
} 
+0

返回字符串的順序是錯誤的(cba而不是abc),但除此之外 - 哇...我需要一段時間來研究它是如何工作的。謝謝 – Ruskin

+1

玩'abc.shift' vs'abc.pop'和'option + item' vs'item + option'來獲取您需要的訂單。 –

+1

@Ruskin沒問題:)如果您對任何特定的代碼行有任何疑問,請不要猶豫,問。代碼確實看起來很棘手。 –

0

這是我做的:

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); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

在哪裏等等是字符矩陣。

醜陋,理所當然,但Yury的短回答傷了我的頭。

+0

感謝波浪 - 不是聰明,但更容易訪問! (和在C#中;)) – Ruskin

+2

嗯,看起來像C#不是JavaScript :) –

+1

可能是C#,但至少它看起來不像Perl! :) – wavydavy

0
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')); 
相關問題