[Python] Python - Steps (1-4) | Non-Overlapping Checker[Python] Python - Steps (1-4) | Non-Overlapping Checker

🐵

Infinite Monkey Theorem

Week 14, 2026

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)