[Jprogramming] Determining an 'alternating' binary array
Marshall Lochbaum
mwlochbaum at gmail.com
Fri Jul 27 15:56:14 UTC 2012
Here's another option: convert the two rows into one using base 2, discard
the ones that are 0, and check if the remaining ones are 1 2 1...
I borrowed the sort in Raul's solution so that they must be in that order,
not 2 1 2... .
alternate =: (-: 1 2$~$)@:(0 -.~ #.@|:)@:/:~
Note that we don't have to convert to binary. In this case, we end up with
alternate =: (-: (0 1,:1 0)$~#)@:(0 0 -.~ |:)@:/:~
Marshall
On Fri, Jul 27, 2012 at 10:39 AM, Brian Schott <schott.brian at gmail.com>wrote:
> Another approach?
>
> alternates =: [: -. 1 e. [: +/ 0 1 |."0 1 ] #"_ 1~ +./
> noAnds =: *./@(-.@(*./))
> check =: noAnds *. alternates
>
>
>
>
> --
> (B=)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
More information about the Programming
mailing list