Created
December 7, 2021 19:33
-
-
Save kvakes/3f1c6e2f86deac5273d2a881943b118e 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 chase_rabbit(): | |
# Setup | |
holes = 100 | |
rabbit_in = random.randrange(0, holes) | |
checks = 0 | |
# Jump | |
def jump(fromHole): | |
if fromHole == 0: | |
return 1 | |
if fromHole == holes - 1: | |
return holes - 2 | |
if bool(random.getrandbits(1)): | |
return fromHole + 1 | |
else: | |
return fromHole - 1 | |
def checkHole(n): | |
nonlocal rabbit_in, checks | |
checks = checks + 1 | |
if n == rabbit_in: | |
print("🐇 found in hole " + str(n)) | |
return True | |
else: | |
#print("hole: " + str(n) + " 🐇: " + str(rabbit_in)) | |
rabbit_in = jump(rabbit_in) | |
return False | |
# Going right (up until penultimate hole) | |
print("Going right...") | |
for n in range(holes - 1): | |
for _ in range(3): | |
if checkHole(n): | |
return checks | |
## Special case of last hole: check exactly twice | |
if checkHole(holes - 1): return checks | |
if checkHole(holes - 1): return checks | |
# Going left | |
print("Going left...") | |
for n in range(holes - 2, -1, -1): | |
if checkHole(n): | |
return checks | |
tests = 100000 | |
sum = 0 | |
max = 0 | |
min = 10000 | |
for x in range(tests): | |
checks = chase_rabbit() | |
sum = sum + checks | |
if max < checks: | |
max = checks | |
if min > checks: | |
min = checks | |
print("Average # of checks:" + str(sum / tests) + ". Max: " + str(max) + ". Min: " + str(min)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment