MathGroup Archive 2002

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

Search the Archive

Re: Generate all k-tuples

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36207] Re: [mg36181] Generate all k-tuples
  • From: BobHanlon at aol.com
  • Date: Tue, 27 Aug 2002 02:07:14 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 8/26/02 5:25:36 AM, rdobrow at carleton.edu writes:


> Is there an easy (elegant?) way to generate the set of all k-tuples
> taking values from some set (list) S? I want the arguments of the
> function to be k (the length of the tuples) and the set S. That is,
> KTuples[3,{a,b}] should produce
> {{a,a,a},{a,a,b},{a,b,a},{a,b,b},{b,a,a},{b,a,b},{b,b,a},{b,b,b}}.
> 

kTuples[n_Integer?Positive, s_List] :=
 
     Flatten[Outer[List, Sequence@@Table[s,{n}]],n-1];


s = {a,b};


kTuples[3, {a,b}]


{{a, a, a}, {a, a, b}, {a, b, a}, {a, b, b}, {b, a, a}, {b, a, b}, {b, b, a}, 
{b, b, b}}


Length[kTuples[10, {a,b}]]


1024


Bob Hanlon
Chantilly, VA   USA



  • Prev by Date: Re: How to use inversefunction
  • Next by Date: Re: Generate all k-tuples
  • Previous by thread: Re: Generate all k-tuples
  • Next by thread: Re: Generate all k-tuples