A bit neater, along the same lines as before, probably more
space hungry (assertion not tested!)
NB. nub of original pair ; (sum if different)
sump =: ~.@; +/^:(=/)
sump each p
+-----+-------+-----+-----+-----+-----+-----+-------+-------+-----+-----+-------+
|+---+|+---+-+|+---+|+---+|+---+|+---+|+---+|+---+-+|+---+-+|+---+|+---+|+---+-+|
||1 2|||2 2|4|||2 4|||4 1|||1 5|||5 3|||3 4|||4 4|8|||4 4|8|||4 2|||2
3|||3 3|6||
|+---+|+---+-+|+---+|+---+|+---+|+---+|+---+|+---+-+|+---+-+|+---+|+---+|+---+-+|
+-----+-------+-----+-----+-----+-----+-----+-------+-------+-----+-----+-------+
NB. much better mask array, a bit like Arie's use of I.
maskp =: I.@:(=/&>) =/ i.@#
NB. put them together, with extra unchanged row
f =: (0,~maskp) {every"1 sump each
f p
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|4 |2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|8 |4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|8 |4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|6 |
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
I tried working on the predecessor to p, without much
success.
Mike
On 07/11/2016 09:55, 'Mike Day' via Programming wrote:
Well, this is pretty clunky, but it seems to work ok on
your data. I noticed that p may be derived from an unboxed
vector pp here, but that might not be relevant, and it's
easier to process p than pp:
pp =: 1 2 2 4 1 5 3 4 4 4 2 3 3
[p =: 2<\pp
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
sump =: 4 : '+/^:x y'NB. sum y if x
0 1 sump"0 1] 2 3 4
2 3 4
9 0 0
NB. form 1 mask row for each non-scalar identical pair
NB. (Assumes all boxes fewer than three items)
maskp =: (=/~>:@i.@(>./))@:(*+/\)@:(=/every)
maskp p
0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1
(maskp sump each "1 ])p
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|4 |2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|8 |4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|8 |4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|6 |
+---+---+---+---+---+---+---+---+---+---+---+---+
NB. You require the original array to be presented last:
(,~(maskp sump each "1 ]))p
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|4 |2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|8 |4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|8 |4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|6 |
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
NB. OR
(((0,~maskp) sump each "1 ]))p
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|4 |2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|8 |4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|8 |4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|6 |
+---+---+---+---+---+---+---+---+---+---+---+---+
|1 2|2 2|2 4|4 1|1 5|5 3|3 4|4 4|4 4|4 2|2 3|3 3|
+---+---+---+---+---+---+---+---+---+---+---+---+
Any use?
Mike
PS: Arie Groeneveld's one-liner has just popped up,
while drafting the above, but I might as well send
this wordy if not worthy alternative!
On 07/11/2016 07:36, Skip Cave wrote:
Trying Louis' program:
sumpears=: +/@]^:[&.>~ (*. 1 = +/\"1)@:(=/@>)
n
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
sumpears n
┌───┬─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│4│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼─┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│4│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼─┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│4│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼─┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│4│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
└───┴─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
So this verb replaces the first pair (2,2) with 4, but it replaces
the SAME
pair in every row. I want it to replace a DIFFERENT pair in each row.
like this:
sumpears n
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│ 4 │2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│ 8 │4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│ 8 │4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│ 6 │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
Also ideally I would like to start out with just one row of boxed pairs:
n
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
The pair replacement process would add a new row for each duplicate
pair,
and stop when all the dup pairs have been replaced by sums.
Skip
Skip Cave
Cave Consulting LLC
On Mon, Nov 7, 2016 at 1:19 AM, Louis de Forcrand <[email protected]>
wrote:
sumpears=: +/@]^:[&.>~ (*. 1 = +/\"1)@:(=/@>)
Does this do what you want?
Louis
Sent from my iPhone
On 07 Nov 2016, at 05:50, Skip Cave <[email protected]> wrote:
NB. I have the following array n consisting of 5 identical rows of
boxed
pairs of integers:
n
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
I want to design a verb f such that it will find boxes containing
duplicate
pairs in n. f will sum the duplicate pairs, and replace the duplicate
with
the sum of the two pairs. However, I want to do this only once for
each
row,
and find a new pair in the subsequent row to replace. See the result
below.
f n
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1 2│ 4 │2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│ 8 │4 4│4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│ 8 │4 2│2 3│3 3│
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│ 6 │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│1 2│2 2│2 4│4 1│1 5│5 3│3 4│4 4│4 4│4 2│2 3│3 3│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
If all duplicates have been found, no change will be made in the row.
Can this be done without control statements?
Skip
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm