The last trick you mentioned (compare with =i.2 and |.=i.2 after filtering
the 0 0's out) is very similar to the second verb I mentioned:

alternate =: (-: (0 1,:1 0)$~#)@:(0 0 -.~ |:)@:/:~

This uses a sort to ensure that we start on 0 1, and then compares the
result with (0 1,1 0,0 1...), a shape n,2 array. A more direct translation
of your intent would use dyad \ , an adverb which takes a number on the
left and applies the verb to chunks of the right argument with that length.
Then you could simply use the verb to compare each with =i.2 or |.=i.2 ,
and *. the results together. Or you could sort and compare to =i.2 , which
is a little quicker. I'll leave the complete verb as an exercise.

Marshall

On Mon, Jul 30, 2012 at 4:12 PM, Peter B. Kessler <
[email protected]> wrote:

> R.E. Boss wrote:
>
>> My 2 cents:
>>
>>    (>./-<./)+/\+/1 _1 *A
>> 1
>>
>>    (>./-<./)+/\+/1 _1 *B
>> 2
>>
>> So A qualifies, B does not.
>>
>>
>> R.E. Boss
>>
>
> This doesn't address the case where both inputs have 1's in the same column
>
>            ]C=:2 5 $ 1 0 0 1 0 0 1 0 1 0
>
>         1 0 0 1 0
>         0 1 0 1 0
>            (>./-<./)+/\+/1 _1 *C
>         1
>
> The original statement of the problem doesn't say what to do with
> simultaneous 1's, but I would think they fail the "alternating" description.
>
> I am a total newb when it comes to J programming, though I did some APL
> programming in the early 1970's.  I find that what's missing from this
> example (and lots of other examples of J programs) is a statement of why it
> is written the way it is.  I find J expressions to be wonderfully precise
> on *what* the expression is doing, but not *why* the author wrote it that
> way.  (E.g., whether it's taking advantage of some special case, or should
> be studied by us newbs for its deep insights.)  It often takes me a long
> time to figure out what a given expression does, then I have to figure out
> how that combination solves the problem at hand, and guess at why that's
> the best way to solve this particular problem.   For example, I love the
> use of "+/\" in the above to propagate the state of the match.  (Though, I
> worry a little about the efficiency of it, if prefix really reapplies the
> verb to each of the possible prefixes, especially as the inputs get long.)
>  Raul Miller and especially Ma
> rshall Lochbaum are wonderful exceptions in this thread, describing how
> their solutions work.
>
> In the "alternating binary array" case, it seems, to me, like what's
> called for is a small finite state machine (or regular expression parser,
> if that's how you think).  Start in a neutral state, and move to a new
> state on reading a column of the input.  I think there are only 4 states:
> TooSoonToTell, MostRecently01, MostRecently10, and Error.  TooSoonToTell
> just moves to one of the MostRecently states based on the input (or to
> Error on 1 1).  The MostRecently states transition to the other
> MostRecently on the "other" input, or to Error on the same input (or on 1
> 1).  Error just stays in Error regardless of the input.  The state machine
> seems almost J-friendly, since it can be represented as a matrix with rows
> for states and columns for inputs, yielding a new state.  I see that J
> *has* a Sequential Machine (dyadic :;) operator.  Why isn't that the right
> solution to the alternating 1's problem?
>
> Conversely, why is it not J-like to filter out the 0 0 columns (and check
> for 1 1 columns while you are there), then match successive 2x2 blocks to
> make sure they are all the same?  Either "2 2 $ 0 1 1 0" or "2 2 $ 1 0 0
> 1", whichever comes first?  (Or use Raul Miller's sort trick to impose an
> order.)  I'm sure it would take me hours to construct that expression, and
> maybe I shouldn't spend the time, if that's not the J way to do things like
> this.  Is that not J-like because I shouldn't be thinking in terms of
> iterating over the input?
>
> I'm trying to learn to think like a J programmer.  Thanks for teaching me
> to fish.
>
>                         ... peter
>
>
>  -----Oorspronkelijk bericht-----
>>> Van: 
>>> programming-bounces@forums.**jsoftware.com<[email protected]>[mailto:
>>> programming-bounces@**forums.jsoftware.com<[email protected]>]
>>> Namens Linda Alvord
>>> Verzonden: zaterdag 28 juli 2012 9:43
>>> Aan: [email protected]
>>> Onderwerp: Re: [Jprogramming] Determining an 'alternating' binary array
>>>
>>> In this case the tacit version leads to a better explicit definition:
>>>
>>>
>>>    al=: 13 :'(-:((2 2$0 1 1 0)$~#))(|: y) -. 0 0'
>>>    al
>>> [: (-: ((2 2$0 1 1 0) $~ #)) 0 0 -.~ |:
>>>
>>>    al2=: 13 :'(-:(2 2$0 1 1 0)$~#)0 0 -.~|:y'
>>>    al2
>>> [: (-: ((2 2$0 1 1 0) $~ #)) 0 0 -.~ |:
>>>
>>> Linda
>>>
>>> -----Original Message-----
>>> From: 
>>> programming-bounces@forums.**jsoftware.com<[email protected]>
>>> [mailto:programming-bounces@**forums.jsoftware.com<[email protected]>]
>>> On Behalf Of Linda Alvord
>>> Sent: Saturday, July 28, 2012 3:14 AM
>>> To: [email protected]
>>> Subject: Re: [Jprogramming] Determining an 'alternating' binary array
>>>
>>> This seems to work as well:
>>>
>>>    (|:A)-: |: A/:A
>>> 1
>>>    al=: 13 :'(-:((2 2$0 1 1 0)$~#))(|: y) -. 0 0'
>>>    al
>>> [: (-: ((2 2$0 1 1 0) $~ #)) 0 0 -.~ |:
>>>
>>>    al A
>>> 1
>>>    al B
>>>
>>> -----Original Message-----
>>> From: 
>>> programming-bounces@forums.**jsoftware.com<[email protected]>
>>> [mailto:programming-bounces@**forums.jsoftware.com<[email protected]>]
>>> On Behalf Of Linda Alvord
>>> Sent: Saturday, July 28, 2012 2:45 AM
>>> To: [email protected]
>>> Subject: Re: [Jprogramming] Determining an 'alternating' binary array
>>>
>>> Here's an alternative to 'alternative'
>>>
>>>     ]A=:2 8$  0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0
>>> 0 0 1 0 0 0 0 1
>>> 1 0 0 0 1 0 0 0
>>>    ]B=:2 5$1 0 0 0 1 0 1 1 0 0
>>> 1 0 0 0 1
>>> 0 1 1 0 0
>>>    alt=: 13 :'(-:((2 2$0 1 1 0)$~#))(|: y /:y) -. 0 0'
>>>    alt
>>> [: (-: ((2 2$0 1 1 0) $~ #)) 0 0 -.~ [: |: /:~
>>>
>>>    alt A
>>> 1
>>>    alt B
>>> 0
>>>
>>> Linda
>>>
>>> -----Original Message-----
>>> From: 
>>> programming-bounces@forums.**jsoftware.com<[email protected]>
>>> [mailto:programming-bounces@**forums.jsoftware.com<[email protected]>]
>>> On Behalf Of Marshall
>>> Lochbaum
>>> Sent: Friday, July 27, 2012 11:56 AM
>>> To: [email protected]
>>> Subject: Re: [Jprogramming] Determining an 'alternating' binary array
>>>
>>> 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
>>> <[email protected]>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 <http://www.jsoftware.com/forums.htm>
>>>>
>>>>  ------------------------------**------------------------------**
>>> ----------
>>> For information about J forums see 
>>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>>>
>>> ------------------------------**------------------------------**
>>> ----------
>>> For information about J forums see 
>>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>>>
>>> ------------------------------**------------------------------**
>>> ----------
>>> For information about J forums see 
>>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>>>
>>> ------------------------------**------------------------------**
>>> ----------
>>> For information about J forums see 
>>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>>>
>>
>> ------------------------------**------------------------------**
>> ----------
>> For information about J forums see 
>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>>
> ------------------------------**------------------------------**----------
> For information about J forums see 
> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to