All problems
Combinations (nCr)
Problem
Compute the number of ways to choose r items from n items without regard to order:
C(n, r) = n! / (r! * (n-r)!)
Return 0 if r < 0 or r > n.
Hint
Use the multiplicative formula: product of (n-i)/(i+1) for i in [0, r).
Test cases
case 1
in: [5,2]
expected: 10
case 2
in: [10,3]
expected: 120
case 3
in: [7,0]
expected: 1
case 4
in: [0,0]
expected: 1
case 5
in: [5,6]
expected: 0
Starter codePython
def combinations(n, r):
# Your code here
pass5 test cases · run your code against them in the editor