As Henry pointed out,
do take a look at [1] for how to approach this in J
If you want to understand my solution,
just try it step by step.
I repeat it here, with a bit of litter removed:
f =: {{ (,~ {&y) {. I. (>: x) e."1 +/\ =/~ y }}
NB. where does each number occur?
=/~ d
1 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 1 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 1 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 1 0 0 1 0
0 0 1 0 0 1 0 0 0 0 0 0 1
NB. cumulative sum of rows:
NB. this number (column) already (row) occurred this often
+/\ =/~ d
1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0 0 0 1
1 1 1 1 0 1 0 0 1 0 0 1 1
1 1 1 1 1 1 0 0 1 0 0 1 1
1 1 2 1 1 2 0 0 1 0 0 1 2
1 1 2 1 1 2 1 0 1 0 0 1 2
1 1 2 1 1 2 1 1 1 0 0 1 2
1 1 2 2 1 2 1 1 2 0 0 2 2
1 1 2 2 1 2 1 1 2 1 0 2 2
1 1 2 2 1 2 1 1 2 1 1 2 2
1 1 2 3 1 2 1 1 3 1 1 3 2
1 1 3 3 1 3 1 1 3 1 1 3 3
NB. the first row with a 2
{. I. 2 e."1 +/\ =/~ d
5
NB. index d with that value
(,~ {&d) {. I. 2 e."1 +/\ =/~ d
2 5
NB. same with 3 (your x value 2)
(,~ {&d) {. I. 3 e."1 +/\ =/~ d
3 11
In the meantime, Raul posted a solution which I guess
will show a better approach.
Hauke
[1] https://code.jsoftware.com/wiki/Essays/Progressive_Index-Of
Am 26.01.22 um 21:49 schrieb Pawel Jakubas:
It should be of course
1 f d
2 5
Would be great if you could decompose your solution and the idea behind the
solution. Many thanks.
Cheers,
Pawel
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
----------------------
mail written using NEO
neo-layout.org
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm