number to unique permutation mapping of a sequence containing duplicates

后端 未结 5 1200
野趣味
野趣味 2021-02-02 00:26

I am looking for an algorithm that can map a number to a unique permutation of a sequence. I have found out about Lehmer codes and the factorial number system thanks to a simila

5条回答
  •  耶瑟儿~
    2021-02-02 01:28

    As I was unsure of the code in gbronner's answer (or of my understanding), I recoded it in R as follows

    ritpermz=function(n, parclass){
        return(factorial(n) / prod(factorial(parclass)))}
    
    rankum <- function(confg, parclass){
        n=length(confg)
        permdex=1
        for (i in 1:(n-1)){
          x=confg[i]
          if (x > 1){
            for (j in 1:(x-1)){
                if(parclass[j] > 0){
                    parclass[j]=parclass[j]-1
                    permdex=permdex + ritpermz(n-i, parclass)
                    parclass[j]=parclass[j]+1}}}
            parclass[x]=parclass[x]-1
        }#}
        return(permdex)
    }
    

    which does produce a ranking with the right range of integers

提交回复
热议问题