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. |
<lang j> A. 2 0 1 NB. return rank of permutation |
||
4 |
|||
3 |
|||
⚫ | |||
⚫ | |||
2 0 1 |
|||
17 |
|||
A. |
(i.@! A. i.) 3 NB. generate all permutations for 3 items |
||
0 1 2 |
|||
3 17 |
|||
0 2 1 |
|||
⚫ | |||
1 0 2 |
|||
1 2 0 |
|||
17 3 A. 0 1 2 3 NB. return permutations of rank 17 and 3 |
|||
2 |
2 0 1 |
||
2 1 0 |
|||
</lang> |
|||
A. (i.@! A. i.) 3 NB. ranks of each permuation |
|||
⚫ | |||
]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}}== |