subset permutations
Anton Vredegoor
anton.vredegoor at gmail.com
Sun Dec 11 10:20:19 EST 2005
Steven D'Aprano wrote:
> On Fri, 09 Dec 2005 16:03:46 +1100, Ben Finney wrote:
>
> >> Do you want the result to be:
> >> AB, AC, AD, BC, BD, CD
> >
> > That is the complete set of combinations of the letters.
> >
> >> Or, do you want AB,BA,AC,CA,AD,DA,BC,CB,BD,DB,CD,DB ?
> >
> > That is the complete set of permutations of the letters.
>
>
> Only if you are choosing without replacement. If you are choosing with
> replacement, you also get AA, BB, CC, DD.
>
> Unfortunately, there is ambiguity in the terms "permutation" and
> "combination", not just between English common usage and mathematics, but
> even in mathematics.
Why is that unfortunate? Now we can all post our favorite scripts and
let them be severely criticized :-)
Anton
def ncycle(seq,n):
while True:
for x in seq:
i = 0
while i < n:
yield x
i += 1
def cross(*args):
p = 1
R = []
for seq in reversed(args):
R.append(ncycle(seq,p))
p *= len(seq)
R.reverse()
i = 0
while i < p:
yield tuple(x.next() for x in R)
i += 1
def test():
s1='a1','a2','a3','a4'
s2='b1','b2'
s3='c1','c2','c3'
for x in cross(s1,s2,s3):
print x
if __name__=='__main__':
test()
More information about the Python-list
mailing list