Сочетания и перестановки
Вычисляйте nCr (сочетания) и nPr (перестановки) с пошаговыми решениями.
Часто задаваемые вопросы
Реализация кода
import math
# Combinations C(n, r): order doesn't matter
n, r = 10, 3
c = math.comb(n, r)
print(f"C({n},{r}) = {c}") # 120
# Permutations P(n, r): order matters
p = math.perm(n, r)
print(f"P({n},{r}) = {p}") # 720
# Manual calculation
def combinations(n, r):
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
def permutations(n, r):
return math.factorial(n) // math.factorial(n - r)
# All actual combinations
from itertools import combinations as combs, permutations as perms
items = ["A", "B", "C", "D"]
for combo in combs(items, 2):
print(combo) # ('A','B'), ('A','C'), ...
for perm in perms(items, 2):
print(perm) # ('A','B'), ('A','C'), ('B','A'), ...
# Combinations with repetition
from itertools import combinations_with_replacement
for c in combinations_with_replacement("ABC", 2):
print(c) # ('A','A'), ('A','B'), ...Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.