/* Lehmer code from permutation in Picat. From https://resources.wolframcloud.com/FunctionRepository/resources/LehmerCodeFromPermutation """ The Lehmer code corresponding to Lehmer code perm is created. ... The generating algorithm is as follows. For each number in the permutation, count how many subsequent values it exceeds. ... LehmerCodeFromPermutation[{1, 2, 9, 5, 4, 12, 7, 6, 3, 11, 10, 8}] {0, 0, 6, 2, 1, 6, 2, 1, 0, 2, 1, 0} """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> A = [1, 2, 9, 5, 4, 12, 7, 6, 3, 11, 10, 8], println(a=A), % X is the Lehmer code lehmer(A, X), % restore the original array A lehmer(Y, X), println(x=X), println(y=Y), % fail, nl. go => true. lehmer(A, X) => if var(A) then N = X.len, A = new_list(N), A :: 1..N, all_different(A) else N = A.len end, X = new_list(N), X :: 0..N, foreach(I in 1..N) X[I] #= sum([(A[J] #< A[I]) : J in I..N]) end, solve($[ff,split],X++A).