MathGroup Archive 2004

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

Search the Archive

Re: Partitioning a list into unequal partitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47686] Re: Partitioning a list into unequal partitions
  • From: Mark Fisher <mark at markfisher.net>
  • Date: Thu, 22 Apr 2004 02:38:30 -0400 (EDT)
  • References: <c5gfdp$an0$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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]]

--Mark.


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