Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*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 2006

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

Search the Archive

Re: generalized foldlist problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69091] Re: generalized foldlist problem
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 30 Aug 2006 06:32:22 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <ed0rja$sll$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Arkadiusz Majka wrote:
> DearAll,
> 
> Please, help!
> 
> I have two list
> 
> list1={a,b,c,d,e}
> list2={3,2,5,1,6}
> 
> and I want to apply a modified version of FoldList to list1 in the
> following way: list2 indicates that element a appears only 3 times (if
> space enough) beginning from the beginning of the list , element b
> appears 2 times, c - 5 times , etc.
> 
> So the output should be
> 
> GeneralizedFoldList[list1,list2]={a,a+b,a+b+c,c+d,c+e}
> 
> Thanks for any hints,
> 
> arek
> 
Hi Arek,

The following code function should be a good start:

GeneralizedFoldList[list1_List, list2_List] /; Length[list1] == 
Length[list2] :=
   Module[
     {lst = Transpose[{list1, list2}],
      len = Length[list1]},

     Plus @@ MapIndexed[PadRight[Table[#1[[1]],
         {#1[[2]]}], len, 0, #2[[1]] - 1] & , lst]
   ]

list1 = {a, b, c, d, e};
list2 = {3, 2, 5, 1, 6};
GeneralizedFoldList[list1, list2]

--> {a, a + b, a + b + c, c + d, c + e}

Regards,
Jean-Marc


  • Prev by Date: Re: generalized foldlist problem
  • Next by Date: Re: a curious answer
  • Previous by thread: Re: generalized foldlist problem
  • Next by thread: Re: generalized foldlist problem