Ric Sherlock wrote:
> 
> I saw the following thread on comp.lang.apl
> http://groups.google.com/group/comp.lang.apl/browse_thread/thread/89c585c9d1a7bd3a#
> and was trying to put together a (non-regex) J solution.
> 
> Given the matrix:
>    ]A=: 'CTGGTTGAT','GTAGTCATA','CATGTCTAA','TCGAAAGTT',:'CCGGAGAAG'
> CTGGTTGAT
> GTAGTCATA
> CATGTCTAA
> TCGAAAGTT
> CCGGAGAAG
> 
> and the list 'TAG'
> 
> Identify the rows of the matrix where the letters in the list occur in
> the order they appear in the list, but not necessarily adjacent to
> each other.
> So the result of a verb findinRows would be:
>    A findinRows 'TAG'
> 0 1 0 1 0
> 
> I have an answer below but am thinking there may be other approaches I'm
> missing
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Spoiler alert!!
> 
> 
> 
> 
> 
> 
> findinRows=: [: (1 e. ,@:(</))&>/ 2 (([: ($ #. ]) 4 $. [: $. </)&.>)/\
>  [: <@I."1 ="_ 0
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 
> 
Here is yet another solution, based on doing reduce operation.
Let's consider:

   chop=.(>:@i. }. [)"1 0

Now the following reduction seems to work:

   A
CTGGTTGAT
GTAGTCATA
CATGTCTAA
TCGAAAGTT
CCGGAGAAG

   'G' chop~ 'A' chop~ 'T' chop~ A
      
TCATA 
      
TT    

Non-"empty" lines contained the ordered sequence (incl. repeated chars)
before the reduction.

The subtle caveat is that if the last char to be matched appears only as the 
last char of a line, it won't be recognized correctly.  Thus the reduction
is 
done for all the chars except the last one, which is looked for after the 
reduction.  Altogether, the solution is:

  findinrows=: {:@] +./@:="0 1 (chop~R |.@}:)

where the reduce adverb R is defined as:

R =: 1 : 0
  u/y
:
  a=.x
  b=.y
  while.#b do.
    a=.a u~{:b
    b=.}:b
  end.
  a
)

      A findinrows 'TAG'
0 1 0 1 0
      A findinrows 'TAA'
0 1 1 1 0
      A findinrows 'TAGT'
0 1 0 1 0
      A findinrows 'TAAG'
0 0 0 1 0
      A findinrows 'AAG'
0 0 0 1 1
      A findinrows 'A'
1 1 1 1 1



-- 
View this message in context: 
http://old.nabble.com/Search-for-ordered-letters-within-lines-of-matrix-tp32287342s24193p32295746.html
Sent from the J Programming mailing list archive at Nabble.com.

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

Reply via email to