Skip to content

Commit

Permalink
[3.13] Add multinomial to the itertools recipes docs (pythongh-129760) (
Browse files Browse the repository at this point in the history
  • Loading branch information
miss-islington authored Feb 7, 2025
1 parent f7af8bc commit aae0a1f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit aae0a1f

Please sign in to comment.