Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Feel free to explore, learn, and share your own approaches.
- [x] **3/24:** Earthquake Anomaly 🌏
- [x] **3/25:** Opening Day ⚾️
- [ ] **3/26:** Flatten Array 🪜
- [ ] **3/26:** ❓❓❓
- [x] **3/26:** [Infinite Monkey Theorem](https://github.com/codedex-io/daily-challenges/tree/main/march-2026/3-27-infinite-monkey-theorem) 🐒
- [ ] **3/28:** ❓❓❓
- [ ] **3/29:** ❓❓❓
- [ ] **3/30:** ❓❓❓
Expand Down
36 changes: 36 additions & 0 deletions march-2026/3-27-infinite-monkey-theorem/infinite-monkey-theorem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def infinite_monkey(target, attempt):
target_len = len(target)

# init vars to track the best window found
best_index = 0
best_score = 0

# slide the window across every valid position
for i in range(len(attempt) - target_len + 1):
window = attempt[i : i + target_len]

# compare each character one by one
matches = 0
for j in range(target_len):
if window[j] == target[j]:
matches += 1

# convert to a percentage score
score = (matches / target_len) * 100

# update best if this window is better
if score > best_score:
best_score = score
best_index = i

# calculate attempts using the formula, or None if score is 0
if best_score == 0:
attempts = None
else:
attempts = round(1 / (best_score / 100) ** target_len)

return {
'best_index': best_index,
'similarity': round(best_score, 2),
'attempts': attempts
}