https://github.com/python/cpython/commit/1581c9d936a665dad9a5481ba880ac3b8fb2b824
commit: 1581c9d936a665dad9a5481ba880ac3b8fb2b824
branch: 3.12
author: Bénédikt Tran <[email protected]>
committer: rhettinger <[email protected]>
date: 2025-02-08T08:10:41-06:00
summary:

[3.12] Add multinomial to the itertools recipes docs (gh-129760) (gh-129854)

files:
M Doc/library/itertools.rst

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:

_______________________________________________
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