What is the "j" expression for the "maximum consecutive ones" problem?

211 views Asked by At

In the paper "Combinatory Logic and Combinators in Array Languages" they give a solution in APL:

      vec ← 1 1 0 1 1 1 0 0 0 1
      ⍝ split (partition) on zeroes
      ⊆⍨vec
┌───┬─────┬─┐
│1 1│1 1 1│1│
└───┴─────┴─┘
      ⍝ size of each sublist
      ≢ ̈⊆⍨vec
2 3 1
      ⍝ max reduction
      ⌈/≢¨⊆⍨vec
3

For clarity, they also note:

The final maximum consecutive ones APL solution can be translated for those who don’t read APL: reduce(max, map(length, W(partition, vec)))

So, how would one express the following in J?

 ⌈/≢¨⊆⍨vec

The symbol seems to be a "partition" operator. It's not clear this exists in J but I may have just missed it. Curious what the above expression would be in "J".

2

There are 2 answers

2
bob On

Dan Bron's answer in the comments is the way to go for sure. I did a video on this problem and if you are interested you can watch me walk through the different options. https://www.youtube.com/watch?v=lbi_PMVbeaQ The version I ended preferring was

t=:[: >./ [: #;. _2 ,&0

but I also look at

t=:[: {: [: $ [: ];. _2 ,&0

as an alternative.

0
jthulhu On

To provide a different point of view, the maximum number of 1s in a row is also the maximum of the difference between consecutive indices of 0s in the array (minus one), for which J (almost) has a primitive: I. (up to a -.). So the following works too:

t=: [: -&1 [: >./ [: 2&(-~/;._3) [: I. -.