MathGroup Archive 1995

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

Search the Archive

Re: Challenge!

A postscript to my last posting:

In article <3nkksu$obd at>,
Dave Wagner <wagner at bullwinkle.cs.Colorado.EDU> wrote:
>In article <3nkc03$mq8 at>,
>Paul E Howland  <PEHOWLAND at> wrote:
>>Dear Mathgroup,
>>I have two lists of equal length M.
>>I wish to generate a third list also of length M, where the i th element
>>of this list is either the i th element of the first list, or the i th
>>element of the second list. 
>Here's my best shot:
>    interleave[list1_List, list2_List] :=
>    	#[[Random[Integer, {1,2}]]]& /@ Transpose[{list1, list2}]

It occurred to me after posting that this technique has the added
advantage of being straightforwardly extensible to an arbitrary
number of lists:

    interleave[s__List] :=
    With[{len = Length[{s}]},
	    #[[Random[Integer, {1,len}]]]& /@ Transpose[{s}]

    sa = Array[a,10];
    sb = Array[b,10];
    sc = Array[c,10];
    sd = Array[d,10];

    Do[Print[interleave[sa, sb, sc, sc]], {10}]

    {b[1], b[2], c[3], c[4], a[5], c[6], c[7], b[8], b[9], b[10]}
    {a[1], c[2], b[3], c[4], a[5], c[6], c[7], c[8], c[9], c[10]}
    {c[1], c[2], c[3], c[4], a[5], c[6], c[7], a[8], b[9], c[10]}
    {c[1], b[2], b[3], c[4], c[5], a[6], a[7], c[8], a[9], c[10]}
    {c[1], c[2], a[3], c[4], c[5], c[6], a[7], a[8], b[9], a[10]}
    {a[1], c[2], a[3], c[4], b[5], c[6], c[7], a[8], a[9], c[10]}
    {b[1], b[2], b[3], c[4], b[5], b[6], c[7], b[8], b[9], c[10]}
    {a[1], b[2], b[3], c[4], c[5], a[6], a[7], b[8], a[9], c[10]}
    {c[1], a[2], b[3], c[4], c[5], c[6], a[7], a[8], a[9], c[10]}
    {b[1], c[2], b[3], c[4], b[5], a[6], c[7], c[8], c[9], c[10]}

		Dave Wagner
		Principia Consulting
		(303) 786-8371
		princon at

  • Prev by Date: Re: Challenge!
  • Next by Date: Re: NDSolve problem
  • Previous by thread: Re: Challenge!
  • Next by thread: Programming: List Structure Manipulation