Created
April 10, 2014 20:39
-
-
Save ghewgill/10421051 to your computer and use it in GitHub Desktop.
Simulation for https://programmers.stackexchange.com/questions/235581/guess-the-slowly-moving-number-with-penalty-for-too-high-guess
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 collections | |
import random | |
MAX = 2**64 | |
def search(weight, target): | |
low = 0 | |
high = MAX | |
time = 0 | |
while True: | |
x = low + (high - low) // weight | |
if x < target: | |
time += 1 | |
low = x + 1 | |
elif x > target: | |
time += 300 | |
high = x - 1 | |
else: | |
return time | |
for w in range(2, 500): | |
a = [search(w, random.randint(0, MAX)) for _ in range(1000)] | |
print "%d,%d" % (w, sum(a) / len(a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment