Project Euler #24: Lexicographic permutations

  • + 0 comments

    Tried to reverse the process of finding the rank of a word in a dictionary
    100/-points

    from math import factorial
    def lint(n):
        if n==int(n):
            return int(n)
        else:
            return int(n)+1
    word='abcdefghijklm'
    li=[]
    for i in sorted(word):
        li.append(i)
    
    def answer_calc(pos,lis):
        answer=''
        for i in range(len(word)-1,-1,-1):
            index=lint(pos/factorial(i))-1
            answer+=lis.pop(index)
    
            pos+=-(index)*factorial(i)
    
        return answer
    for _ in range(int(input())):
        n=int(input())
        print(answer_calc(n,li[:]))