mramine364

Rank permutation

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