• + 0 comments

    Python Method : Create dict to store the element as key and the index+1 of p as value. For example, [5,2,1,3,4] we can get {5:1, 2:2, 1:3, 3:4, 4:5} and use p_dict[p_dict[1]] to get the answer 4 . It can reduce the computing of list.index().

    def permutationEquation(p):
        p_dict = {j:i+1 for i,j in enumerate(p)}
        y = []
        for x in range(1,len(p)+1):
            y.append(p_dict[p_dict[x]])
        return y