Display elements by the number of occurrences in J

Using the J language, I want to achieve a mapping of the counts of the elements of an array.

In particular, I want to enter a lowercase word with two different letters and return each pair of letters to the word along with counts of occurrences.

I need a verb that gives something like this in any J-structure that you think is appropriate:

For 'cocoa':

co 2
oc 1
oa 1

For a banana:

ba 1
an 2
na 2

For "milk":

mi 1
il 1
lk 1

For 'to':

to 1

(For single-letter words, such as 'a', the task is undefined and no attempt will be made.)

(The order is not important, since I have listed them.)

I can easily get consecutive pairs of letters in a word as a matrix or a list of fields:

   2(] ;._3)'cocoa'
co
oc
co
oa
   ]
   2(< ;._3)'cocoa'
β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”
β”‚coβ”‚ocβ”‚coβ”‚oaβ”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜

But I need help to go from there to mapping pairs to numbers.

~. ~: . .

NuVoc "Loopless" , /(/\. /\) - , , / , u/y , , , y.

(NB. "" , Java Python, . SO J. J .)

+4
2

, dyadic infix (\):

   2 ]\ 'banana'
ba
an
na
an
na

, , key (/.)

   #/.~ 2 ]\ 'banana'
1 2 2

, :

   ({. ; #)/.~ 2 ]\ 'banana'
β”Œβ”€β”€β”¬β”€β”
β”‚baβ”‚1β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚anβ”‚2β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚naβ”‚2β”‚
β””β”€β”€β”΄β”€β”˜
+6

, . , .

 [t=. 2(< ;._3)'cocoa'
β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”
β”‚coβ”‚ocβ”‚coβ”‚oaβ”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜

~. (Nub),

   ~.t
β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”
β”‚coβ”‚ocβ”‚oaβ”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜

, -, , 1 - ,

   t =/ ~.t
1 0 0
0 1 0
1 0 0
0 0 1

, ,

    +/  t =/ ~.t
2 1 1

,

   <"0 +/  t =/ ~.t
β”Œβ”€β”¬β”€β”¬β”€β”
β”‚2β”‚1β”‚1β”‚
β””β”€β”΄β”€β”΄β”€β”˜

, , ,. (Stitch)

 (~.t) ,. <"0 +/  t =/ ~.t
β”Œβ”€β”€β”¬β”€β”
β”‚coβ”‚2β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚ocβ”‚1β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚oaβ”‚1β”‚
β””β”€β”€β”΄β”€β”˜
       [t=. 2(< ;._3)'banana'
β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β”
β”‚baβ”‚anβ”‚naβ”‚anβ”‚naβ”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
   (~.t) ,. <"0 +/  t =/ ~.t
β”Œβ”€β”€β”¬β”€β”
β”‚baβ”‚1β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚anβ”‚2β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚naβ”‚2β”‚
β””β”€β”€β”΄β”€β”˜
   [t=. 2(< ;._3)'milk'
β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”
β”‚miβ”‚ilβ”‚lkβ”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
   (~.t) ,. <"0 +/  t =/ ~.t
β”Œβ”€β”€β”¬β”€β”
β”‚miβ”‚1β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚ilβ”‚1β”‚
β”œβ”€β”€β”Όβ”€β”€
β”‚lkβ”‚1β”‚
β””β”€β”€β”΄β”€β”˜

, .

+2

Source: https://habr.com/ru/post/1678971/


All Articles