Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*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 2002

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

Search the Archive

Re: A friendly challenge: Generalized Partition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34932] Re: A friendly challenge: Generalized Partition
  • From: gleam at flashmail.com ("Mr. Wizard")
  • Date: Thu, 13 Jun 2002 02:38:23 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Since it seems you're all coming up with code pretty much identical to
mine, as well as to each other's, I see no point in waiting to post
the code I had previously arrived at.  My fastest was:

f[l_,p_]:=Take[l,#]&/@c@p
c=Compile[{{p,_Integer,1}},Transpose@{#~Drop~-1+1,Rest@#}&@FoldList[Plus,0,p]];

And and shortest:

f[l_,p_]:=Block[{v=0},l~Take~{v+1,v+=#}&/@p]

I compiled the larger part of the first function shortly before posing
this challenge, but doesn't seem to have provided any real advantage,
so I am going back a version without it for my own use.  I was using
length and sum checking to replicate some of the functionality of
ggCheckArgs, in this final form:

dynamicPartition[l_List,p_List]/;Length@l>=Tr@p:=
  Take[l,#]&/@Transpose@{#~Drop~-1+1,Rest@#}&@FoldList[Plus,0,p]

a more secure check, as (roughly) suggested by Hartmut Wolf, would be:

dynamicPartition[l_List,p:{_Integer?NonNegative..}]/;Length@l>=Tr@p:=
  Take[l,#]&/@Transpose@{#~Drop~-1+1,Rest@#}&@FoldList[Plus,0,p]

Paul



  • Prev by Date: Re: PolynomialQ ?
  • Next by Date: Re: Re: which Linux does Mathematica run on?
  • Previous by thread: Re: A friendly challenge: Generalized Partition
  • Next by thread: Re: Re: A friendly challenge: Generalized Partition