Lesson 7 of 15
Basic Counting
The Art of Counting
Combinatorics answers the question: how many ways can something happen?
Fundamental Rules
Product rule: If task A can be done in ways and task B in ways independently, together there are ways.
Sum rule: If task A can be done in ways OR task B in ways (mutually exclusive), there are ways total.
Permutations and Combinations
Permutation — ordered arrangements of items from :
Combination — unordered selections of items from :
Arrangements with repetition — choices from options with replacement:
import math
def permutations(n, r):
return math.factorial(n) // math.factorial(n - r)
def combinations(n, r):
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
def arrangements_with_repetition(n, r):
return n ** r
print(permutations(5, 3)) # 60 (5×4×3)
print(combinations(10, 3)) # 120
Your Task
Implement permutations(n, r), combinations(n, r), and arrangements_with_repetition(n, r).
Pyodide loading...
Loading...
Click "Run" to execute your code.