diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index a82fd8630b7b6a..b1b7b630a5ecbb 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -1082,6 +1082,12 @@ The following recipes have a more mathematical flavor: n -= n // prime return n + def multinomial(*counts): + "Number of distinct arrangements of a multiset." + # Counter('abracadabra').values() -> 5 2 1 1 2 + # multinomial(5, 2, 1, 1, 2) → 83160 + return math.prod(map(math.comb, accumulate(counts), counts)) + .. doctest:: :hide: @@ -1644,6 +1650,12 @@ The following recipes have a more mathematical flavor: >>> ''.join(it) 'DEF1' + >>> multinomial(5, 2, 1, 1, 2) + 83160 + >>> word = 'coffee' + >>> multinomial(*collections.Counter(word).values()) == len(set(permutations(word))) + True + .. testcode:: :hide: