Wednesday, April 20, 2011

First team to win N games

This is just a quick post for "my" TEAP students who couldn't solve one problem and I promised to show some code (I normally don't do that ;-).



Problem
Two teams (A and B) are competing against each other. They proceed to play games in sequence until one of them wins exactly N times. This team is said to be the winner of the sequence.
We have the probability P of the team A winning a single game. Also there can never be a draw.
Given P and N, calculate the probability of A winning the game.

Solution
The simplest way to solve this is by recursion. We define a function that calculates the solution after A won a games and B won b games. I used Python to get the exact solution. Code:

def solve(a,b,N,P):
  #left something out here ;-) 
  return solve(a+1,b,N,P)*(P/Decimal(100)) + 
         solve(a,b+1,N,P)*(Decimal(1)-P/Decimal(100))

See how cool Python is? I believe that you can manage the rest of the solution on your own ;-)

No comments:

Post a Comment