Come to think of it, the interpreter does recognize the patterns ;@(<;.f;.1) and [: ; <@f;.1 and has done so since J5.03. http://www.jsoftware.com/books/help/release/cut503.htm
f=: ;@:(<@(+/\);.1) g=: [: ; <@(+/\);.1 v=: 1e6$0 1 1 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 1 (f -: rdm) v 1 (f -: rdm) v 1 (g -: rdm) v 1 ts=: 6!:2 , 7!:[EMAIL PROTECTED] ts 'rdm v' 0.175548 1.36323e7 ts 'f v' 0.0852698 4.19507e6 ts 'g v' 0.0864283 4.19507e6 f1=: (1 (0)} 0 = ]) ;@(<@(+/\);.1) ] (f -: f1) v 1 ts 'f1 v' 0.0453225 5.24365e6 f1 works on any (non-empty) numeric list, boolean or integer or real or complex, and is the fastest. ----- Original Message ----- From: "Roger Hui" <[EMAIL PROTECTED]> To: "J Programming Forum" <[email protected]> Sent: Wednesday, April 26, 2006 8:27 AM Subject: re: [Jprogramming] Puzzle: Stop and go sums Comments on the solutions. It is not clear from the original specifications that the argument must be boolean. For non-Boolean arguments: db1 and rdm fail on arguments with negative or complex values. reb fails on arguments with values less than _2 or greater than 1, or with nonzero fractions, or complex values. dg fails if the leading atom is not 0. A couple of variants of the solution dg, the monad or dyad ; @ (<@(+/\);.1) [: ; <@(+/\);.1 has the potential for being the fastest if the interpreter recognized the patterns. ---------------------------- Raul Miller wrote on Monday, April 24: Dan Bron wrote: > sgs 0 1 1 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 1 > 0 1 2 3 0 0 0 0 1 2 0 0 0 1 2 3 4 5 0 1 > > What's the most efficient way to express sgs Here's timing for the various implementations which have been posted so far: db0 =: (~:&0@:[ * +)/\.&.|. db1 =: ;@:(<@:(+/\);.1~ (< _1&(|.!._))) rdm=: -. (] - [: >./\ *) +/\ reb=: (0:`+ at .[)/\.&.|. dg=:[:;[:+/\&.><;.1 v=: 1e6$0 1 1 1 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 1 time=:4 :0 '`verb ignore'=.x`] if._1<nc<'prior'do.assert(prior-:verb)y end. smoutput (5!:5<'verb'),TAB,":6!:2 'verb y' prior=:verb ,.'' ) db0`db1`rdm`reb`dg time"0 1 v db0 2.43829 db1 0.446227 rdm 0.0384559 reb 2.22763 dg 0.796831 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
