Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-147

Ballot Problem Probability

Problem

The ballot problem: candidate A receives a votes and candidate B receives b votes, and votes are counted in random order. Return the probability that A is strictly ahead of B throughout the count:

P = (a - b) / (a + b).

Return 0.0 when a < b or the total is 0.

Hint

The result is the reflection principle count divided by the total count.

Test cases

case 1
in: [6,4]
expected: 0.2
case 2
in: [5,3]
expected: 0.25
case 3
in: [3,3]
expected: 0
case 4
in: [4,6]
expected: 0
Starter codePython
def ballot_probability(a, b):
    # Your code here
    pass

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