28 august 2009

Wide cut

Sometimes one needs to cut a bit more than the actual delimiter shows. An example will explain.
Suppose the array 0 1 1 1 0 1 0 0 2 2 2 1 2 2 0 2 1 1 2 1 0 2 0 1 2 0 0 is to be boxed after a 2 including the next zeroes with a max of 2 and after a 1 including the next zero, if possible. The output should be

+---+-+---+---+---+-+-+-+-+---+-+-+-+-+---+---+-+-----+
|0 1|1|1 0|1 0|0 2|2|2|1|2|2 0|2|1|1|2|1 0|2 0|1|2 0 0|
+---+-+---+---+---+-+-+-+-+---+-+-+-+-+---+---+-+-----+

To do this a technique is used applied by Hui in Hui 2008July.

Translating the requirements in a more concise form we get the following cases to be considered

- 1 0 -      NB. - can be everything, so 0 1 2
- 2 0 *      NB. * is unequal to 0, so 1 2
2 0 0 -
- - 1 *
- - 2 *

Notice that we have to look at 4 positions since a decision cannot be made after 2 0 or after 1 or after 2 .
If we apply 3&#.  to the 4 numbers, these conditions become

- 1 0 -      9 10 11  e.~ 27 | 3 #. ]
- 2 0 *       19 20   e.~ 27 | 3 #. ]
2 0 0 -      54 55 56 e.~      3 #. ]
- - 1 *        4 5    e.~  9 | 3 #. ]
- - 2 *        7 8    e.~  9 | 3 #. ]

NB. So together

   ((9 10 11 19 20 e.~ 27 | ]) +. (4 5 7 8 e.~ 9 |] ) +. 54 55 56 e.~ ]) 3 #. ]

Since we replace the third position of every infix of 4, the argument has to be preceded by 0 0 and followed by 2 or 1 .
So the verb to produce the wide cut becomes

wicu=: 3 : 'y <;.2~ ((9 10 11 19 20 e.~ 27 | ]) +. (4 5 7 8 e.~ 9 |] ) +. 54 55 56 e.~ ]) 3 #. 4[\0 0 , y , 2'

   wicu 0 1 1 1 0 1 0 0 2 2 2 1 2 2 0 2 1 1 2 1 0 2 0 1 2 0 0
+---+-+---+---+---+-+-+-+-+---+-+-+-+-+---+---+-+-----+
|0 1|1|1 0|1 0|0 2|2|2|1|2|2 0|2|1|1|2|1 0|2 0|1|2 0 0|
+---+-+---+---+---+-+-+-+-+---+-+-+-+-+---+---+-+-----+

RE Boss/J-blog/WideCut (last edited 2009-08-31 16:59:42 by RE Boss)