Python SolutionsPython Solutions
🐵
Infinite Monkey Theorem
Week 14, 2026
All SolutionsPython One-Pass Solution | Alexevi | Python Solutions
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()
Python - Steps (1-4) | Non-Overlapping Checker | BMC | Python Solutions
def sortByLen(aList:list[str]) -> None:
aList.sort(key=len)
def step1(aList:list[str], aDict:dict) -> None:
for word in aList:
aDict.update({word : 0})
def step2(aList:list[str], aDict:dict) -> None:
newList = list({len(word): word for word in aList}.values())
for word in newList:
aDict.update({len(word) : 0})
def step3(aWord:str) -> None:
global wordsLenDict, wordsDict, randomMonkeyString
count = randomMonkeyString.count(aWord)
wordsDict[aWord] += count
if count > 0:
wordsLenDict[len(aWord)] += count
addWordToWordList(aWord)
def step4() -> None:
global wordsLenDict, wordsDict, wordList, ans
for i, j in wordsLenDict.items():
unique_words = sorted(wordList[i-1])
lineOfWords = ""
firstWord = True
for word in unique_words:
lineOfWords += f" {word}"
ans.append(f"--- {i}-Letter Words ---")
ans.append(f"Total Count: {j}")
ans.append(f"Unique Count: {len(wordList[i-1])}")
ans.append(f"Unique Words:{lineOfWords}")
ans.append("")
def addWordToWordList(aWord:str) -> None:
global wordList
if len(aWord) < 1:
return
wordList[len(aWord)-1].append(aWord)
def writeToFile(aList:list[str]) -> None:
with open("ans.txt", 'w') as file:
for line in aList:
file.write(line+'\n')
# main
# -- main vars --
wordsLenDict = {}
wordsDict = {}
wordsStrict = []
wordList = []
randomMonkeyString = ""
ans = []
# -- read files --
with open("words-strict.txt", 'r') as file:
wordsStrict = file.read().split()
with open("random-monkey-string.txt", 'r') as file:
randomMonkeyString = file.readline().rstrip('\n')
#randomMonkeyString = "thisabcisdefmonkeyghibusiness"
# -- get sorted list by length --
sortByLen(aList=wordsStrict)
'''
What I want to do is basically is to put all words in wordsDict from wordsStrict
1. Make words len dict
2. Loop through each word in wordsStrict and add count to wordsDict[word] by checking count in randomMonkeyString
3. increment wordsLenDict based on wordsLenDict[len(word)]
4. print in format
'''
print("[STEP-1]")
step1(wordsStrict, wordsDict)
print("[STEP-2]")
step2(wordsStrict, wordsLenDict)
# -- setting wrodList to
wordList = [[] for _ in range(next(reversed(wordsLenDict)))]
# -- finally getting the answer --
Counter = 0
for word in wordsStrict:
if len(word) > 8: # pre-cached check
print("BREAK CONDITION REACHED")
break
print(f"[STEP-3][{Counter}] Word: {word}")
step3(word)
Counter += 1
print("[STEP-4]")
step4()
print("[Writing to file]")
writeToFile(ans)