Hi,

The version I know from
https://github.com/kevinlawler/kona/wiki/Idioms
is

mss=: [: >./ (0 >. +)/\.

It fails on lists of length 0 or 1 but is very concise; the following works on 
all list lengths but has a few extra characters:

mss1=: [: >./ [: (0 >. +)/\. ,&0

which is an almost literal translation of the K version.
It basically takes the maximum over i of subsequence sums starting at i (the K 
version does the same with subsequences sums ending at i).
I believe this is the same as Kadane's algorithm (but don't take my word for 
it).
While very cute, it doesn't include starting index and length, and things get a 
bit uglier if you want those as well:

mssi=: [: ((] , {~) [: (i. >./) {:"1) [: 0"0^:(0 >: {:)@:+/\. 1&,. , 0:    NB. 
returns starting index, length, and value

While mssi performs the same steps as mss1, keeping track of indices defeats 
the beauty of tacit J code and IMO is better done with explicit verbs.
Perhaps someone can find a prettier tacit version of mssi?

Cheers,
Louis
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to