Message 79373 - Python tracker (original) (raw)
Results from Magma, Sage and Gap:
Magma provides functions "Subsets(S, k)" and "Permutations(S, k)". From the documentation:
Subsets(S, k) : The set of subsets of the set S of size k. If k is larger than the cardinality of S then the result will be empty.
Permutations(S, k) : The set of permutations (stored as sequences) of each of the subsets of the set S of cardinality k.
GAP has the same behaviour: even if you've never encountered GAP before, it's fairly Pythonesque, so the following extract from a GAP 4 REPL means exactly what you think it does:
gap> Combinations([1,2,3,4], 3); [ [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 3, 4 ], [ 2, 3, 4 ] ] gap> Combinations([1,2,3,4], 5); [ ]
Permutations work the same way (but the function is called Arrangements):
gap> Arrangements([1,2,3,4], 3); [ [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 3, 2 ], [ 1, 3, 4 ], [ 1, 4, 2 ], [ 1, 4, 3 ], [ 2, 1, 3 ], [ 2, 1, 4 ], [ 2, 3, 1 ], [ 2, 3, 4 ], [ 2, 4, 1 ], [ 2, 4, 3 ], [ 3, 1, 2 ], [ 3, 1, 4 ], [ 3, 2, 1 ], [ 3, 2, 4 ], [ 3, 4, 1 ], [ 3, 4, 2 ], [ 4, 1, 2 ], [ 4, 1, 3 ], [ 4, 2, 1 ], [ 4, 2, 3 ], [ 4, 3, 1 ], [ 4, 3, 2 ] ] gap> Arrangements([1,2,3,4], 5); [ ]
Combinations([1,2,3,4], -1) gives an error. Interestingly, Arrangements([1,2,3,4], -1) returns the empty list.
GAP also has a NrCombinations function returning the number of combinations:
gap> NrCombinations([1,2,3,4], 5); 0 gap> NrCombinations([1,2,3,4], -1); 0
My Sage installation currently seems to be broken, but from the documentation it looks as though it just steals the functions from GAP.