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)) if len(bids) == 1: print "For A=%d" % bids[0], bestBids, 'with average payout', ','.join([("%.5f" % x).rstrip('0') for x in averagePayout]) return bestBids, averagePayout bids, payout = getBest([], NUM_PLAYERS) print "BEST = ",bids, ','.join([("%.5f" % x).rstrip('0') for x in payout])
Standard input is empty
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