Advertisement
kevinsf90

CodeSprint2 - Coin Toss

Jan 8th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. # A python implementation of a solution to the Coin Toss
  2. # problem for Codesprint 2
  3. # Author: Kevin Tham
  4.  
  5. def parseInput(inputString):
  6.     inputList = map(int, inputString.split())
  7.     N = inputList[0]
  8.     M = inputList[1]
  9.     return N, M
  10.        
  11. def solve(N, M=0):
  12.     if M >= N:
  13.         return 0
  14.     elif M==0:
  15.         # Reduce to original N consecutive heads problem
  16.         return pow(2, N+1) - 2
  17.     else:
  18.         # Must toss 1 more + if we get heads + if we get tails
  19.         return 1 + solve(N, M+1)/2 + solve(N, 0)/2
  20.  
  21. def main():
  22.     for eachCase in xrange(int(raw_input())):
  23.         N, M = parseInput(raw_input())
  24.         print str(solve(N,M)) + ".00"
  25.        
  26. if __name__ == '__main__':
  27.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement