@@ -278,22 +278,34 @@ def test_combinatorics(self):
278278 # Test relationships between product(), permutations(),
279279 # combinations() and combinations_with_replacement().
280280
281- s = 'ABCDE'
282- for r in range (8 ):
283- prod = list (product (s , repeat = r ))
284- cwr = list (combinations_with_replacement (s , r ))
285- perm = list (permutations (s , r ))
286- comb = list (combinations (s , r ))
287-
288- self .assertEquals (len (prod ), len (s )** r )
289- self .assertEquals (prod , sorted (set (prod ))) # prod in lexicographic order without repeats
290- self .assertEquals (cwr , [t for t in prod if sorted (t )== list (t )]) # cwr: prods which are sorted
291- self .assertEquals (perm , [t for t in prod if len (set (t ))== r ]) # perm: prods with no dups
292- self .assertEqual (comb , [t for t in perm if sorted (t )== list (t )]) # comb: perms that are sorted
293- self .assertEqual (comb , [t for t in cwr if len (set (t ))== r ]) # comb: cwrs without dups
294- self .assertEqual (comb , list (filter (set (cwr ).__contains__ , perm ))) # comb: perm that is a cwr
295- self .assertEqual (comb , list (filter (set (perm ).__contains__ , cwr ))) # comb: cwr that is a perm
296- self .assertEqual (comb , sorted (set (cwr ) & set (perm ))) # comb: both a cwr and a perm
281+ for n in range (6 ):
282+ s = 'ABCDEFG' [:n ]
283+ for r in range (8 ):
284+ prod = list (product (s , repeat = r ))
285+ cwr = list (combinations_with_replacement (s , r ))
286+ perm = list (permutations (s , r ))
287+ comb = list (combinations (s , r ))
288+
289+ # Check size
290+ self .assertEquals (len (prod ), n ** r )
291+ self .assertEquals (len (cwr ), (fact (n + r - 1 ) / fact (r )/ fact (n - 1 )) if n else (not r ))
292+ self .assertEquals (len (perm ), 0 if r > n else fact (n ) / fact (n - r ))
293+ self .assertEquals (len (comb ), 0 if r > n else fact (n ) / fact (r ) / fact (n - r ))
294+
295+ # Check lexicographic order without repeated tuples
296+ self .assertEquals (prod , sorted (set (prod )))
297+ self .assertEquals (cwr , sorted (set (cwr )))
298+ self .assertEquals (perm , sorted (set (perm )))
299+ self .assertEquals (comb , sorted (set (comb )))
300+
301+ # Check interrelationships
302+ self .assertEquals (cwr , [t for t in prod if sorted (t )== list (t )]) # cwr: prods which are sorted
303+ self .assertEquals (perm , [t for t in prod if len (set (t ))== r ]) # perm: prods with no dups
304+ self .assertEqual (comb , [t for t in perm if sorted (t )== list (t )]) # comb: perms that are sorted
305+ self .assertEqual (comb , [t for t in cwr if len (set (t ))== r ]) # comb: cwrs without dups
306+ self .assertEqual (comb , list (filter (set (cwr ).__contains__ , perm ))) # comb: perm that is a cwr
307+ self .assertEqual (comb , list (filter (set (perm ).__contains__ , cwr ))) # comb: cwr that is a perm
308+ self .assertEqual (comb , sorted (set (cwr ) & set (perm ))) # comb: both a cwr and a perm
297309
298310 def test_compress (self ):
299311 self .assertEqual (list (compress ('ABCDEF' , [1 ,0 ,1 ,0 ,1 ,1 ])), list ('ACEF' ))
0 commit comments