Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-234

Gambler's Ruin Against Infinite Capital

Problem

A gambler with start units plays against an infinitely rich opponent, winning one unit with probability p each round and being ruined when the capital reaches 0. Return the probability of eventual ruin: 1.0 when start <= 0 or p <= 0.5, 0.0 when p >= 1, and ((1 - p) / p) raised to start otherwise.

Hint

With no upper boundary the ruin probability is (q / p)^start when p exceeds 0.5.

Test cases

case 1
in: [1,0.75]
expected: 0.3333333333333333
case 2
in: [3,0.75]
expected: 0.03703703703703703
case 3
in: [5,0.6]
expected: 0.13168724279835398
case 4
in: [2,0.5]
expected: 1
case 5
in: [4,1]
expected: 0
Starter codePython
def gamblers_ruin_infinite(start, p):
    # Your code here
    pass

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