One more solution: cs=: [: <. [ %~ [: # ] b1=: 0 = cs | ] b2=: ] < [ * cs f=: (b1*.b2) < ;. 1 ]
2006/11/15, June Kim <[EMAIL PROTECTED]>:
2006/11/15, June Kim <[EMAIL PROTECTED]>: > from http://c2.com/cgi/wiki?CodingProblem > > Given K, answer an ordered collection of K ordered collections of size > approximately N / K, such that the first contains the first N / K > objects, the second the second N / K, etc. > > There is a J solution on that page but it doesn't work in all cases. > > Following is my solution (with one issue though): > c=: [: <. [ %~ [: # ] > hd=: ([: <: [) * c > > b1=: hd {.] > b2=: hd }.] > f=: <@b2 ,~ [EMAIL PROTECTED] <\ b1 > > assert (0 1 2;3 4 5;6 7 8)-:3 f i.9 > assert ((,:0);1 2)-:2 f i.3 > assert (0 1 2;3 4 5;6 7 8 9)-:3 f i.10 > > assert ((0$<''),<0 1)-: 1 f i.2 NB. $ > 1 f i.2 is 1 2, not 2 > as I expected > > I don't like the last line -- that problem occurs once in a while. How > could I make it to pass "assert (<0 1)-:1 f i.2" with a simple > modification? In other words, assert (,:<0 1)-: 1 f i.2 Hm... Maybe I was wrong. My solution's result is more consistent than how I wanted the result to be. The return value of f is, an array of rank 1. Each item of the array is of rank 1. Then my previous solution is more consistent. I am sometimes confused(and make mistakes) with the fact that in J we have scalars of which rank is empty, which is different from a rank-1 array of a single item. Even a boxed value can a scalar or a rank-1 array(say <1 and ,:<1). > > I am interested in seeing other J solutions. > Still.
---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
