Permutations/Rank of a permutation: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: Start J solution)
(→‎{{header|J}}: complete first 4 tasks)
Line 51: Line 51:
The J primitive <code>A.</code> provides an effective solution to this task.
The J primitive <code>A.</code> provides an effective solution to this task.


<lang j> A. 0 2 3 1 NB. return rank of permutation
<lang j> A. 2 0 1 NB. return rank of permutation
4
3
4 A. i.3 NB. return permutation of rank 4
A. 2 3 1 0
2 0 1
17
A. 0 2 3 1 ,: 2 3 1 0 NB. return rank of the two permutations
(i.@! A. i.) 3 NB. generate all permutations for 3 items
0 1 2
3 17
0 2 1
17 A. 0 1 2 3 NB. return permutation of rank 17
2 3 1 0
1 0 2
1 2 0
17 3 A. 0 1 2 3 NB. return permutations of rank 17 and 3
2 3 1 0
2 0 1
2 1 0
</lang>
A. (i.@! A. i.) 3 NB. ranks of each permuation
0 1 2 3 4 5
]ranks=: 4 ? !12 NB. 4 random numbers sampled from integers 0 to 12!
315645285 249293994 432230943 23060830
ranks A. i.12 NB. 4 random samples of 12 items
7 10 11 8 4 0 2 3 9 5 6 1
6 2 8 11 10 0 5 9 7 1 3 4
10 9 1 2 0 3 8 6 7 5 11 4
0 7 4 6 11 5 10 3 9 8 1 2</lang>


=={{header|Python}}==
=={{header|Python}}==