Created
July 4, 2024 17:54
-
-
Save evaporei/4aa9f21743991ea98a6e16c924ea94bc to your computer and use it in GitHub Desktop.
Fox/Rabbit in a hole problem
This file contains hidden or 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
from random import randint | |
holes = 100 | |
# rabbit jump (pulo do gato) | |
def step(prev_pos: int) -> int: | |
if prev_pos == 0: | |
return prev_pos + 1 | |
elif prev_pos == (holes - 1): | |
return prev_pos - 1 | |
else: | |
direction = randint(0, 1) | |
if direction == 0: | |
return prev_pos - 1 | |
return prev_pos + 1 | |
guesses = [ | |
# 1, 2, 3, ..., n - 1 | |
range(1, holes - 1), | |
# n - 1, n - 2, n - 3, ..., 1 | |
range(holes - 2, 0, -1), | |
] | |
def find() -> bool: | |
pos = step(randint(0, holes - 1)) | |
print('initial pos', pos) | |
for guess in guesses: | |
for attempt in guess: | |
if attempt == pos: | |
print('found rabbit at', pos) | |
return True | |
pos = step(pos) | |
print('new pos', pos) | |
return False | |
print('Found:', find()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Links: