diff --git a/README.md b/README.md index 2525f52..2bea05e 100644 --- a/README.md +++ b/README.md @@ -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:** ❓❓❓ diff --git a/march-2026/3-27-infinite-monkey-theorem/infinite-monkey-theorem.py b/march-2026/3-27-infinite-monkey-theorem/infinite-monkey-theorem.py new file mode 100644 index 0000000..07486ed --- /dev/null +++ b/march-2026/3-27-infinite-monkey-theorem/infinite-monkey-theorem.py @@ -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 + } \ No newline at end of file