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
