NUM_VALUES = 10 NUM_PLAYERS = 3 values = range(1,NUM_VALUES+1) def getPayout(bids): payout = [0]*len(bids) for i in values: distMin = len(values) distances = [0]*len(bids) for j,bid in enumerate(bids): distances[j] = abs(i - bid) if distances[j] < distMin: distMin = distances[j] count = 0 for j,dist in enumerate(distances): if dist == distMin: distances[j] = 1 count += 1 else: distances[j] = 0 for j,pay in enumerate(distances): payout[j] += (float(distances[j]) / float(count)) / float(len(values)) return payout def getBest(bids, playerCount): if len(bids) == playerCount: return bids, getPayout(bids) bestBids = [] bestResult = 0 bestPayouts = [] for bid in values: currBids, payout = getBest(bids +[bid], playerCount) if abs(payout[len(bids)] - bestResult) < 0.0000001: #floating point error tolerance bestPayouts.append(payout) elif payout[len(bids)] > bestResult: bestBids = currBids bestPayouts = [payout] bestResult = payout[len(bids)] averagePayout = [0] * len(bestPayouts[0]) for payout in bestPayouts: for i,val in enumerate(payout): averagePayout[i] += val / float(len(bestPayouts)) return bestBids, averagePayout for NUM_VALUES in range(1,17): values = range(1,NUM_VALUES+1) bids, payout = getBest([], NUM_PLAYERS) print NUM_VALUES,": BEST = ",bids, ','.join([("%.5f" % x).rstrip('0') for x in payout])
Standard input is empty
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