Re: Challenge!
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg904] Re: Challenge!
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Sun, 30 Apr 1995 03:38:32 -0400
- Organization: University of Colorado, Boulder
In article <3nkc03$mq8 at news0.cybernetics.net>, Paul E Howland <PEHOWLAND at taz.dra.hmg.gb> 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. > >It should be equally probable that the new element be chosen from the >first or second list. > > (...) > >What is the most compact function that could achieve my goal? This could be done quite straightforwardly using recursion, but it's best to avoid recursion when possible. Here's my best shot: In[167]:= interleave[list1_List, list2_List] := #[[Random[Integer, {1,2}]]]& /@ Transpose[{list1, list2}] In[168]:= s1 = {a,b,c,d,e,f,g}; s2 = {A,B,C,D,E,F,G}; In[170]:= Do[Print[interleave[s1, s2]], {10}] {a, b, c, D, e, f, g} {A, B, c, d, e, f, g} {a, b, C, D, e, F, G} {A, b, C, D, E, F, g} {a, b, c, D, E, F, G} {A, B, c, D, E, f, G} {a, b, C, d, E, F, g} {A, B, c, D, e, f, g} {A, b, C, d, e, f, G} {A, b, c, D, e, F, g} Dave Wagner Principia Consulting (303) 786-8371 princon at csn.net http://www.csn.net/princon