RE: Generate all k-tuples
- To: mathgroup at smc.vnet.net
- Subject: [mg36227] RE: [mg36181] Generate all k-tuples
- From: "David Park" <djmp at earthlink.net>
- Date: Tue, 27 Aug 2002 02:07:39 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Bob,
KTuples[n_Integer?Positive, elements_List] :=
Flatten[Outer[List, Sequence @@ Table[elements, {n}]], n - 1]
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}}
Outer produces an array of all the elements but we have to flatten it to get
it down to a two-level array.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Bob [mailto:rdobrow at carleton.edu]
To: mathgroup at smc.vnet.net
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}}.