Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created July 4, 2024 17:54
Show Gist options
  • Save evaporei/4aa9f21743991ea98a6e16c924ea94bc to your computer and use it in GitHub Desktop.
Save evaporei/4aa9f21743991ea98a6e16c924ea94bc to your computer and use it in GitHub Desktop.
Fox/Rabbit in a hole problem
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