Last active
August 29, 2015 14:26
-
-
Save livinginformation/70ae3f2a57ecba4387b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def init_game(): | |
global stack, wallet, max_taken, oracle_turns | |
stack = list(range(1, 10001)) | |
random.shuffle(stack) | |
wallet = max_taken = oracle_turns = 0 | |
def read(): | |
return stack[0] | |
def take(): | |
global wallet, max_taken | |
amount = stack.pop(0) | |
if amount > max_taken: | |
wallet += amount | |
max_taken = amount | |
def passe(): | |
stack.pop(0) | |
def oracle(M): | |
global oracle_turns | |
if oracle_turns > 0: | |
raise ValueError("Used Oracle too soon") | |
if M > len(stack) + 1: | |
raise ValueError("Not enough envelopes left") | |
oracle_turns = M | |
return sum(stack[1:M+1])/M | |
def test(algo, N=50): | |
global oracle_turns | |
results = [] | |
for rep in range(N): | |
init_game() | |
for i in range(10000): | |
oracle_turns -= 1 | |
algo() | |
results.append(wallet) | |
print(sorted(results)[N//2]) | |
#Simplist algo, always takes | |
def algo_simple(): | |
take() | |
test(algo_simple) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment