fork download
  1. NUM_VALUES = 10
  2. NUM_PLAYERS = 3
  3.  
  4. values = range(1,NUM_VALUES+1)
  5.  
  6. def getPayout(bids):
  7. payout = [0]*len(bids)
  8. for i in values:
  9. distMin = len(values)
  10. distances = [0]*len(bids)
  11. for j,bid in enumerate(bids):
  12. distances[j] = abs(i - bid)
  13. if distances[j] < distMin:
  14. distMin = distances[j]
  15. count = 0
  16. for j,dist in enumerate(distances):
  17. if dist == distMin:
  18. distances[j] = 1
  19. count += 1
  20. else:
  21. distances[j] = 0
  22. for j,pay in enumerate(distances):
  23. payout[j] += (float(distances[j]) / float(count)) / float(len(values))
  24. return payout
  25.  
  26.  
  27. def getBest(bids, playerCount):
  28. if len(bids) == playerCount:
  29. return bids, getPayout(bids)
  30. bestBids = []
  31. bestResult = 0
  32. bestPayouts = []
  33. for bid in values:
  34. currBids, payout = getBest(bids +[bid], playerCount)
  35. if abs(payout[len(bids)] - bestResult) < 0.0000001: #floating point error tolerance
  36. bestPayouts.append(payout)
  37. elif payout[len(bids)] > bestResult:
  38. bestBids = currBids
  39. bestPayouts = [payout]
  40. bestResult = payout[len(bids)]
  41.  
  42. averagePayout = [0] * len(bestPayouts[0])
  43. for payout in bestPayouts:
  44. for i,val in enumerate(payout):
  45. averagePayout[i] += val / float(len(bestPayouts))
  46.  
  47. return bestBids, averagePayout
  48.  
  49.  
  50.  
  51. for NUM_VALUES in range(1,17):
  52. values = range(1,NUM_VALUES+1)
  53. bids, payout = getBest([], NUM_PLAYERS)
  54. print NUM_VALUES,": BEST = ",bids, ','.join([("%.5f" % x).rstrip('0') for x in payout])
  55.  
Success #stdin #stdout 2.12s 8968KB
stdin
Standard input is empty
stdout
1 : BEST =  [1, 1, 1] 0.33333,0.33333,0.33333
2 : BEST =  [1, 2, 1] 0.375,0.375,0.25
3 : BEST =  [1, 2, 2] 0.33333,0.33333,0.33333
4 : BEST =  [2, 3, 1] 0.375,0.375,0.25
5 : BEST =  [2, 4, 2] 0.36667,0.36667,0.26667
6 : BEST =  [2, 5, 2] 0.375,0.375,0.25
7 : BEST =  [2, 6, 3] 0.35714,0.35714,0.28571
8 : BEST =  [2, 7, 3] 0.34375,0.34375,0.3125
9 : BEST =  [3, 7, 3] 0.37037,0.37037,0.25926
10 : BEST =  [3, 8, 3] 0.375,0.375,0.25
11 : BEST =  [3, 9, 4] 0.36364,0.36364,0.27273
12 : BEST =  [3, 10, 4] 0.35417,0.35417,0.29167
13 : BEST =  [4, 10, 4] 0.37179,0.37179,0.25641
14 : BEST =  [4, 11, 4] 0.375,0.375,0.25
15 : BEST =  [4, 12, 5] 0.36667,0.36667,0.26667
16 : BEST =  [4, 13, 5] 0.35938,0.35938,0.28125