mramine364

combinations

May 29th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function comb(tab, n){
  4.     if( n==1 )
  5.         return get_uniqs_tab(tab);
  6.     var i=0, j, res = [];
  7.     while( i<tab.length-n+1 ){
  8.         var tab2 = comb(tab.slice(i+1),n-1);
  9.         for(j=0;j<tab2.length;j++){
  10.             res.push( [tab[i]].concat(tab2[j]) );
  11.         }
  12.         var tmp = tab[i];
  13.         while( tab[i]==tmp ) i++;
  14.     }
  15.     return res;
  16. }
  17.  
  18. function get_uniqs_tab(t){
  19.     var res = [];
  20.     res.push([t[0]]);
  21.     for(var i=1;i<t.length;i++){
  22.         if( !includes(res,[t[i]]) ){
  23.             res.push([t[i]]);
  24.         }
  25.     }
  26.     return res;
  27. }
  28.  
  29. function includes(tab,t){ // tab 2x2
  30.     for(var i=0;i<tab.length;i++){
  31.         for(var j=0;j<tab[i].length;j++){
  32.             if(tab[i][j]==t[j])
  33.                 return true;
  34.         }
  35.     }
  36.     return false;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment