Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-336

At Least One Fixed Point Probability

Problem

The probability that a uniform random permutation of n items has at least one fixed point is 1 - !n / n! with derangement recurrence !0 = 1, !1 = 0, !n = (n - 1)(!(n-1) + !(n-2)).

Given n >= 1, return the probability.

Hint

Complement of the probability of a derangement.

Test cases

case 1
in: [1]
expected: 1
case 2
in: [4]
expected: 0.625
case 3
in: [6]
expected: 0.6319444444444444
Starter codePython
def at_least_one_fixed_point_probability(n):
    # Your code here
    pass

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