All problems
Derangement Probability
Problem
The probability that a uniform random permutation of n items has no fixed point is !n / n!, where !n is the derangement count with !0 = 1 and !n = (n - 1)(!(n-1) + !(n-2)).
Given n, return the probability.
Hint
The probability approaches 1/e as n grows.
Test cases
case 1
in: [3]
expected: 0.3333333333333333
case 2
in: [4]
expected: 0.375
case 3
in: [5]
expected: 0.36666666666666664
Starter codePython
def derangement_probability(n):
# Your code here
pass3 test cases · run your code against them in the editor