[Python] Python One-Pass Solution[Python] Python One-Pass Solution

🐵

Infinite Monkey Theorem

Week 14, 2026

def main() -> None: file = open("words-strict.txt") words = file.read().split('\n') file.close() dictionary = {} maximum = 0 for w in words: l = len(w) dictionary.update({w: False}) if l > maximum: maximum = l counts = [0] * maximum unique_words: list[list[str]] = [] for i in range(maximum): unique_words.append([]) file = open("random-monkey-string.txt") string = file.read() file.close() length = len(string) for i in range(length): for l in range(maximum): if i+l > length: break word = string[i : i+l] try: if not dictionary[word]: unique_words[l-1].append(word) dictionary[word] = True counts[l-1] += 1 except: pass for i in unique_words: i.sort() for i in range(maximum): if len(unique_words[i]) > 0: print(f"--- {i+1}-Letter Words ---") print("Total Count:", counts[i]) print("Unique Count:", len(unique_words[i])) print("Unique Words:", end='') for word in unique_words[i]: print(f" {word}", end='') print('\n') main()