> -----Original Message-----
> From: [email protected] [mailto:programming-
> [email protected]] On Behalf Of Raul Miller
> Sent: Thursday, August 04, 2011 2:24 PM
> To: Programming forum
> Subject: Re: [Jprogramming] complicated sums of subarrays
> 
> On Wed, Aug 3, 2011 at 9:37 AM, R.E. Boss <[email protected]> wrote:
> >   A=. 0 1 1 2 3 3 3 4 4 5 , 2 2 3 4 4 4 5 5 , 3 4 5 6  , 4
> >   B=. 14 16 8 6 5 8 6 16 16 19 13 12 3 1 9 12 17 0 9 5 17 7 9
> >
> > Required:
> > array C, by taking array B and add each element of B, corresponding
> > with element x in A, to all the next elements of B which correspond to
> > elements y in A with x<y and such that for all z in A placed between x
> > and y, you have x<z.
> 
> > 14 30 22 28 33 36 34 50 50 69 35 34 37 38 46 49 66 49 43 48 65 72 52
> 
> I am having a problem understanding this description.
[...]
> Perhaps I should just ignore the description and work from the code?
> I presume that that is what Stefano has done?

I'll admit I reverse-engineered the requirements from the provided solution
because I felt a bit confused by the task description. The task description
helped me understand that there was a sum involved, but then I couldn't
quite figure out the rest. I blamed my lack of practice with mathematical
propositions.... But it didn't take me long to understand what was going on
because I'd seen something similar not long ago. And, indeed, the solution
was the same.

Basically, an element in B percolates (via a sum) to all the elements whose
depth (indicated by A) is greater than its own depth. This applies only to
the last-so-far of the elements at that depth.
For instance: 16 and 8 are both child of 14 and they get their value summed
to that of their father in the result C.
6 is child of 8, so it gets summed to both its father and its grandfather
(14 at depth 0 + 8 at depth 1).
After the first element with depth 5, there's another one with depth 2. That
one is a direct child of the second 1 (which had value 8). The one following
is, again, child of that same 8. And so on.
In the task description the "comma" to define A seems to separate subtrees
that spawn at ever-growing depths. But for the solution I provided, that's
not an important detail. 
If R.E. Boss had concluded its vector A with a 1 (removing all the commas),
everything would still have worked.

--
Stefano

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

Reply via email to