Re: Generate all k-tuples
- To: mathgroup at smc.vnet.net
- Subject: [mg36241] Re: Generate all k-tuples
- From: Szikla <szszikla at dpg.hu>
- Date: Wed, 28 Aug 2002 04:16:01 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Selwyn Hollis <slhollis at earthlink.net> wrote: > Here's my contestant: > > <<DiscreteMath`Combinatorica` > > KTuples[k_Integer, vals_List] := > Union[KSubsets[PadRight[vals, k*Length[vals], vals], k]] Dear mathgroup, Here is another solution; probably less elegant, but I found it much faster (DiscreteMath's Subsets and KSubsets are too slow. Besides, it produces each tuple only once): KTuples2[n_Integer, L_List] := Flatten[Outer[Append, KTuples2[n-1, L], L, 1], 1] /; n > 1; KTuples2[1, L_List] := Transpose[{L}] (Both function yields the tuples in lexicographic order.) Best Regards, Sz. Szikla