Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-004

Permutations (nPr)

Problem

Compute the number of ways to arrange r items from n items (order matters):

P(n, r) = n! / (n-r)!

Return 0 if r < 0 or r > n.

Hint

Product of the top r terms of n!: n * (n-1) * ... * (n-r+1).

Test cases

case 1
in: [5,2]
expected: 20
case 2
in: [10,3]
expected: 720
case 3
in: [5,0]
expected: 1
case 4
in: [5,5]
expected: 120
case 5
in: [3,5]
expected: 0
Starter codePython
def permutations(n, r):
    # Your code here
    pass

5 test cases · run your code against them in the editor