Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys,math
- BOTTOM = -1
- def expectation_G_Bach(RED_TOTAL, BLACK_TOTAL):
- # sys.stdout = open("C:\\Users\\user\\Desktop\cardgame.txt", "w")
- memo = [[BOTTOM for x in range(RED_TOTAL + 1)] for x in range(BLACK_TOTAL + 1)]
- for r in range(RED_TOTAL + 1):
- memo[r][0] = r
- for b in range(BLACK_TOTAL + 1):
- memo[0][b] = 0
- for r in range(1, RED_TOTAL + 1):
- for b in range(1, BLACK_TOTAL + 1):
- memo[r][b] = max(0, b/(b+r)*(memo[r][b-1] - 1) + r/(b+r)*(memo[r-1][b] + 1))
- # print("%10.4f" % memo[r][b], end="")
- # print('')
- # print('RED: %d \t BLACK: %d \t\t exp: %f' % (RED_TOTAL, BLACK_TOTAL, memo[r][b]))
- # sys.stdout.close()
- # sys.stdout = sys.__stdout__
- return memo
- def expectation_user223935(RED_TOTAL, BLACK_TOTAL):
- memo = [[BOTTOM for x in range(RED_TOTAL + 1)] for x in range(BLACK_TOTAL + 1)]
- for r in range(RED_TOTAL + 1):
- memo[r][0] = 0
- for b in range(BLACK_TOTAL + 1):
- memo[0][b] = b
- for r in range(1, RED_TOTAL + 1):
- for b in range(1, BLACK_TOTAL + 1):
- memo[r][b] = max(b-r, b/(b+r)*memo[r][b-1] + r/(b+r)*memo[r-1][b])
- # print("%10.4f" % memo[r][b], end="")
- # print('')
- # print('RED: %d \t BLACK: %d \t\t exp: %f' % (RED_TOTAL, BLACK_TOTAL, memo[r][b]))
- # sys.stdout.close()
- # sys.stdout = sys.__stdout__
- return memo
- RED = 30
- BLACK = 30
- memo_G_Bach = expectation_G_Bach(RED,BLACK)
- memo_user223935 = expectation_user223935(RED,BLACK)
- equal = True
- for r in range(RED + 1):
- for b in range(BLACK + 1):
- if not math.isclose(memo_G_Bach[r][b], memo_user223935[b][r]):
- print('(r,b) = (%d,%d)\t\t G_Bach: %f \t\t user223935: %f' % (r,b,memo_G_Bach[r][b], memo_user223935[b][r]))
- equal = equal and math.isclose(memo_G_Bach[r][b], memo_user223935[r][b])
- print('tables equal: %s' % equal)
Advertisement
Add Comment
Please, Sign In to add comment