mramine364

permutations

May 29th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getperm(tab){
  2.     var t = perm(tab);
  3.     for(var i=0;i<t.length;i++){
  4.         console.log(t[i])
  5.     }
  6. }
  7.  
  8. function perm(tab){
  9.     var res = [];
  10.     if( tab.length==1 ){
  11.         res.push(tab);
  12.         return res;
  13.     }  
  14.     var uniqs = get_uniqs(tab), i,j;
  15.     for(i=0;i<uniqs.length;i++){
  16.         var tab2 = get_tab_except(tab, uniqs[i]);
  17.         var tab3 = perm(tab2);
  18.         for(j=0;j<tab3.length;j++){
  19.             tab3[j].unshift(uniqs[i])
  20.             res.push(tab3[j]);
  21.         }
  22.     }
  23.     return res;
  24. }
  25.  
  26. function get_uniqs(t){
  27.     var res = [];
  28.     res.push(t[0]);
  29.     for(var i=1;i<t.length;i++){
  30.         if( !res.includes(t[i]) ){
  31.             res.push(t[i]);
  32.         }
  33.     }
  34.     return res;
  35. }
  36.  
  37. function get_tab_except(t, e){
  38.     var res = [], i=0;
  39.     while( t[i]!=e ){
  40.         res.push(t[i]);
  41.         i++;
  42.     }i++;
  43.     while( i<t.length ){
  44.         res.push(t[i]);
  45.         i++;
  46.     }
  47.     return res;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment