MathGroup Archive 2004

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

Search the Archive

Re: Partitioning a list into unequal partitions

For what it's worth, here's my way:

UnevenPartition::usage = "UnevenPartition[list, lengths] returns the 
list partitioned into groups with the specified successive lengths."

UnevenPartition[list_, lengths_] :=
	First@Fold[Regroup, {{}, list}, lengths]

Regroup[{a_, b_}, n_] := {Append[a, Take[b, n]], Drop[b, n]}

UnevenPartition[Range[10], Range[4]]


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

  • Prev by Date: Re: Dashing option and Plot
  • Next by Date: Re: Simplify using custom function
  • Previous by thread: Re: Partitioning a list into unequal partitions
  • Next by thread: Regarding a 3D plot in mathematica