MathGroup Archive 2004

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

Search the Archive

Re: All combinations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47375] Re: All combinations
  • From: astanoff_otez_ceci at yahoo.fr (astanoff)
  • Date: Wed, 7 Apr 2004 03:16:33 -0400 (EDT)
  • References: <c4u3ih$9lc$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

János wrote:
> Hi,
> I need in a list of all 'k' length combinations of set of 'n' different 
> elements where k can be smaller or bigger than n.  For example if I 
> have n=4 and the set is {a,b,c,d} then all combinations of k=7 length 
> would be
> {{a,a,a,a,a,a,a}
> {a,a,a,a,a,a,b}
> {a,a,a,a,a,b,a}
> ....
> ....
> {d,d,d,d,d,d,d}}
[...]
Janos,
I couldn't find any builtin solution, but I had fun writing this :
In[1]:=
repetitivePermutations[elements_List, k_Integer]:=
    With[{lg = Length[elements]},
      IntegerDigits[#,lg,k]& /@ Range[0,lg^k-1] /. Thread[
          Range[0,lg-1] -> elements]];

Example :
repetitivePermutations[{a,b,c,d},7]

hth
--
v.a.
--
0% de pub! Que du bonheur et des vrais adhérents !
Vous aussi inscrivez-vous sans plus tarder!!
Message posté à partir de http://www.gyptis.org, BBS actif depuis 1995.




  • Prev by Date: Relaunch of Mathematica in Education & Research
  • Next by Date: Re: Assigning elements to a matrix
  • Previous by thread: Re: All combinations
  • Next by thread: Re: All combinations