문제

Are there really simple ways to accomplish something similar to the Anagram Index (A.) functions on an "n choose k" permutation in J?

So, I love A. in J. Just to show that I understand it slightly:

   A. 0 3 4 2 1
17
   17 A. i.5
0 3 4 2 1

I also know that I can use it to permute a list (17 A. things) or use the results to select from a list, if that's any different ((17 A. i.5) { things).

I'm looking for a function that will produce a number similar to A. and also do the inverse to recreate the selection for the "n choose k" permutations, both with and without replacement. Order does matter to me, so 0 2 7 is different than 7 0 2, but I'd be interested in versions that don't care about order (such as when rolling dice in Yahtzee).

I'm prepared to start figuring this out on my own and writing my own function, but I just have to assume that this is a solved problem, especially in a language like J that includes A. as a primitive.

Just to be clear, by "n choose k," I'm talking about rolling dice ("6 choose 3, with replacement" for rolling 3 dice, one after another) and horse race winners ("20 choose 3, without replacement" for the top 3 horses in a 20-horse race).

도움이 되었습니까?

해결책

For "6 choose 3, with replacement", you can use catalog {:

{3#<i.6
┌─────┬─────┬─────┬─────┬─────┬─────┐
│0 0 0│0 0 1│0 0 2│0 0 3│0 0 4│0 0 5│
├─────┼─────┼─────┼─────┼─────┼─────┤
│0 1 0│0 1 1│0 1 2│0 1 3│0 1 4│0 1 5│
├─────┼─────┼─────┼─────┼─────┼─────┤
...

You can take the reverse as the index of the above matrix (unraveled), feg:

(0 0 0) I.~ >,{3#<i.6
0
(0 1 0) I.~ >,{3#<i.6
6
(5 5 5) I.~ >,{3#<i.6
215
(4 5 1) I.~ >,{3#<i.6
175

175 { >,{3#<i.6
4 5 1

There are some relevant essays on combinations on jsoftware.com: Combination Index, Combinations and others.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top