Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rank_perm(t){
- var st=t.slice(), uniqs=get_uniqs(st), j, res=0;
- while( st.length!=1 ){
- j=0;
- while( j<uniqs.length ){
- if( uniqs[j]<st[0] )
- res += nbr_perm( get_tab_except(st, uniqs[j]) );
- j++;
- }
- uniqs = get_tab_except( uniqs, st.shift() );
- }
- return res;
- }
- function nbr_perm(t) {
- t.sort();
- var a=1, i=2; while( i<=t.length ){ a*=i; i++; }
- var b=1,j,fact;
- i=0;
- while( i<t.length-1 ){
- j=fact=1;
- while( t[i]==t[i+1] ){ j++;i++;fact*=j; }
- b*=fact; i++;
- }
- return a/b;
- }
- function get_tab_except(t, e){
- var res = [], i=0;
- while( i<t.length && t[i]!=e){
- res.push(t[i]);
- i++;
- }i++;
- while( i<t.length ){
- res.push(t[i]);
- i++;
- }
- return res;
- }
- function get_uniqs(t){
- var res = [];
- res.push(t[0]);
- for(var i=1;i<t.length;i++){
- if( !res.includes(t[i]) ){
- res.push(t[i]);
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment