fork(9) 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. if len(bids) == 1:
  47. print "For A=%d" % bids[0], bestBids, 'with average payout', ','.join([("%.5f" % x).rstrip('0') for x in averagePayout])
  48. return bestBids, averagePayout
  49.  
  50.  
  51.  
  52. bids, payout = getBest([], NUM_PLAYERS)
  53. print "BEST = ",bids, ','.join([("%.5f" % x).rstrip('0') for x in payout])
  54.  
  55.  
Success #stdin #stdout 0.1s 9016KB
stdin
Standard input is empty
stdout
For A=1 [1, 8, 2] with average payout 0.225,0.425,0.35
For A=2 [2, 8, 3] with average payout 0.3,0.4,0.3
For A=3 [3, 8, 3] with average payout 0.375,0.375,0.25
For A=4 [4, 8, 3] with average payout 0.25,0.45,0.3
For A=5 [5, 7, 4] with average payout 0.15,0.45,0.4
For A=6 [6, 4, 7] with average payout 0.15,0.45,0.4
For A=7 [7, 3, 8] with average payout 0.25,0.45,0.3
For A=8 [8, 3, 3] with average payout 0.375,0.375,0.25
For A=9 [9, 3, 4] with average payout 0.3,0.4,0.3
For A=10 [10, 3, 4] with average payout 0.225,0.425,0.35
BEST =  [3, 8, 3] 0.375,0.375,0.25