MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Partitioning a list into unequal partitions


*This message was transferred with a trial version of CommuniGate(tm) Pro*
On 13 Apr 2004, at 19:26, DIAMOND Mark R. wrote:

> Could someone please show me a simple (non-procedural) way of 
> partitioning a
> list into 1,2,3 ... n disjoint sublists, where the length of the list 
> is
> guaranteed to be correct (i.e. n*(n+1)/2)
>
> I can't see a way, and yet I'm sure there *must* be a one-liner using 
> Fold.
>
> Cheers
>
> Mark R. Diamond
>
>
>
Well, it kind of depends on the meaning of "simple" and 
"non-procedural". For example, what does the follwoing qualify:


f[l_List] := (Take[l, #1] & ) /@
   Table[{(1/2)*i*(i + 1) + 1, (1/2)*(i + 1)*
        (i + 2)}, {i, 0, (1/2)*
        (-3 + Sqrt[1 + 8*Length[l]])}]

This will work provided your list is of the right length. For example:


l=Range[6*7/2]

Out[73]=
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}


f[l]


{{1},{2,3},{4,5,6},{7,8,9,10},{11,12,13,14,15},{16,17,18,19,20,21}}


Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: Partitioning a list into unequal partitions
  • Next by Date: Re: Length of actual parameters in a function call.
  • Previous by thread: Partitioning a list into unequal partitions
  • Next by thread: Re: Partitioning a list into unequal partitions (Thanks)