Skip to content

Commit 1581c9d

Browse files
authored
[3.12] Add multinomial to the itertools recipes docs (gh-129760) (gh-129854)
1 parent 7ff8e8d commit 1581c9d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Doc/library/itertools.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,12 @@ The following recipes have a more mathematical flavor:
10821082
n -= n // prime
10831083
return n
10841084

1085+
def multinomial(*counts):
1086+
"Number of distinct arrangements of a multiset."
1087+
# Counter('abracadabra').values() -> 5 2 1 1 2
1088+
# multinomial(5, 2, 1, 1, 2) → 83160
1089+
return math.prod(map(math.comb, accumulate(counts), counts))
1090+
10851091

10861092
.. doctest::
10871093
:hide:
@@ -1644,6 +1650,12 @@ The following recipes have a more mathematical flavor:
16441650
>>> ''.join(it)
16451651
'DEF1'
16461652

1653+
>>> multinomial(5, 2, 1, 1, 2)
1654+
83160
1655+
>>> word = 'coffee'
1656+
>>> multinomial(*collections.Counter(word).values()) == len(set(permutations(word)))
1657+
True
1658+
16471659

16481660
.. testcode::
16491661
:hide:

0 commit comments

Comments
 (0)