All problems
Gambler Ruin Expected Duration
Problem
The expected number of steps until ruin or victory from i in a walk to 0 or n is i (n - i) when p = 0.5. For p != 0.5 it is (i - n (1 - r^i) / (1 - r^n)) / (1 - 2p) with r = (1 - p) / p.
Given i, n, and p, return the expected duration.
Hint
The fair-coin case is the parabola i (n - i).
Test cases
case 1
in: [1,5,0.5]
expected: 4
case 2
in: [2,4,0.4]
expected: 3.8461538461538476
case 3
in: [3,6,0.7]
expected: 6.405405405405406
Starter codePython
def gambler_ruin_expected_duration(i, n, p):
# Your code here
pass3 test cases · run your code against them in the editor