Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg47487] RE: [mg47454] Partitioning a list into unequal partitions
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 14 Apr 2004 07:16:35 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: DIAMOND Mark R. [mailto:dot at dot.dot]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, April 13, 2004 12:26 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg47487] [mg47454] Partitioning a list into unequal partitions
>
>
>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
>
>
>
>

One possibility would be to factor a prototype and the just take parts:
 
In[4]:= tt = Array[a, {31}]

In[5]:=
tt[[#]] & /@ {{1}, {2, 3}, {4, 5, 6}, {7, 8, 9, 10}, {11, 12, 13, 14, 
    15}, {16, 17, 18, 19, 20, 21}, {22, 23, 24, 25, 26, 27, 28}, {29, 30, 
    31}}
Out[5]=
{{a[1]}, {a[2], a[3]}, {a[4], a[5], a[6]}, {a[7], a[8], a[9], a[10]},
{a[11], 
    a[12], a[13], a[14], a[15]}, {a[16], a[17], a[18], a[19], a[20], 
    a[21]}, {a[22], a[23], a[24], a[25], a[26], a[27], a[28]}, {a[29],
a[30], 
    a[31]}}


This is an attempt with Fold:

In[22]:= Fold[{Sequence @@ Drop[#1, -1], Take[#1[[-1]], {1, #2}], 
    Drop[#1[[-1]], {1, #2}]} &, {tt}, {1, 2, 3, 4, 5, 6, 7}]
Out[22]=
{{a[1]}, {a[2], a[3]}, {a[4], a[5], a[6]}, {a[7], a[8], a[9], a[10]},
{a[11], 
    a[12], a[13], a[14], a[15]}, {a[16], a[17], a[18], a[19], a[20], 
    a[21]}, {a[22], a[23], a[24], a[25], a[26], a[27], a[28]}, {a[29],
a[30], 
    a[31]}}

(You may use this to make your prototype. You may also automatically
generate rules for replacements)

--
Hartmut Wolf


  • Prev by Date: Re: Regarding a 3D plot in mathematica
  • Next by Date: Re: fixed point _convergence _check
  • Previous by thread: Re: Partitioning a list into unequal partitions
  • Next by thread: Re: Partitioning a list into unequal partitions