Re: Q: extract all k-tuple from a list of n elements
- To: mathgroup at smc.vnet.net
- Subject: [mg49658] Re: Q: extract all k-tuple from a list of n elements
- From: astanoff_otez_ceci at yahoo.fr (astanoff)
- Date: Tue, 27 Jul 2004 07:00:35 -0400 (EDT)
- References: <ce2fv9$8rm$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Klaus Duellmann wrote:
> Question: How can I extract all k-tuple from a list of n elements
> (without considering permutations of the k-tuple)?
> Example: For the special case k=3 one solution would be
> Flatten[Table[{i, j, k}, {i, 1, n - 2}, {j, i + 1, n - 1}, {k, j + 1,
> n}], 2];
> A generalization of this solution for all k >=1 would involve to create
> 'automatically' a table of dimension k, but how can this be implemented?
> Any help is appreciated.
> Regards,
> Klaus
--
A colleague of mine gave me this recursive solution
(seems rather cryptic but it works) :
tuple[k_Integer /; k>1]:=
tuple[k]=
Union[Sort /@ ((Function[{x},{Sequence@@First[x],#}& /@ Last[x]] /@
({#,DeleteCases[tuple[1],Alternatives@@#]}& /@ tuple[k-1]))
//Flatten[#,1]&)]
test :
tuple[1]={"a","b","c","d","e"};
tuple[3]
{{a,b,c},{a,b,d},{a,b,e},{a,c,d},{a,c,e},{a,d,e},{b,c,d},{b,c,e},{b,d,e},{c,d,
e}}
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.