Advertisement
Guest User

tuesday puzzle 4

a guest
Dec 17th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. from fractions import Fraction
  2.  
  3. def max_chance(blacks, reds):
  4.   stand = Fraction(blacks, blacks + reds)
  5.   if not blacks or not reds: return stand
  6.   draw = (stand * max_chance(blacks - 1, reds) +
  7.           (Fraction(1,1) - stand) * max_chance(blacks, reds - 1))
  8.   print '%d\t%d\t%s\t%s\t' % (blacks, reds, stand, draw),
  9.   if draw == stand: print 'TIE'
  10.   if draw > stand: print 'DRAW'
  11.   if draw < stand: print 'STAND'
  12.   return max(stand, draw)
  13.  
  14. class Memoize:
  15.   def __init__(self, f):
  16.     self.f = f
  17.     self.memo = {}
  18.   def __call__(self, *args):
  19.     if not args in self.memo:
  20.       self.memo[args] = self.f(*args)
  21.     return self.memo[args]
  22.  
  23. max_chance = Memoize(max_chance)
  24.  
  25. print 'blacks\treds\tstand\tdraw\tchoice'
  26. print max_chance(26, 26)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement