https://github.com/python/cpython/commit/aae0a1f90441467b27a1e7bce03b2aeb98e70ab3
commit: aae0a1f90441467b27a1e7bce03b2aeb98e70ab3
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: rhettinger <[email protected]>
date: 2025-02-06T18:45:15-06:00
summary:

[3.13] Add multinomial to the itertools recipes docs (gh-129760) (gh-129762)

files:
M Doc/library/itertools.rst

diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index e487fc4d553580..8e78ae9cc00f0b 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -838,10 +838,10 @@ and :term:`generators <generator>` which incur 
interpreter overhead.
 
 .. testcode::
 
-   from collections import deque
+   from collections import Counter, deque
    from contextlib import suppress
    from functools import reduce
-   from math import sumprod, isqrt
+   from math import comb, prod, sumprod, isqrt
    from operator import itemgetter, getitem, mul, neg
 
    def take(n, iterable):
@@ -1127,6 +1127,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 prod(map(comb, accumulate(counts), counts))
+
 
 .. doctest::
     :hide:
@@ -1730,6 +1736,12 @@ The following recipes have a more mathematical flavor:
     >>> ''.join(it)
     'DEF1'
 
+    >>> multinomial(5, 2, 1, 1, 2)
+    83160
+    >>> word = 'coffee'
+    >>> multinomial(*Counter(word).values()) == len(set(permutations(word)))
+    True
+
 
 .. testcode::
     :hide:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to