alphabet = sorted([*"qwertyuiopasdfghjklzxcvbnm"]) def gen_word(i1, i2, i3, i4): return alphabet[i1] + alphabet[i2] + alphabet[i3] + alphabet[i4] with open("/usr/share/dict/words", "r") as file: word_list = [x[:-1] for x in file.readlines() if len(x) == 5] print(len(word_list)) # print(word_list) best_offset = (0,0,0,0) # conceptually, we hold the outer ring constant and find all permutations in the inner rings # for each one, we do the full 26-letter rotation, generating the words for i in range(26): # print(f"i = {i}") for j in range(26): # print(f" j = {j}") for k in range(26): # print(f" k = {k}") words = [] for outer in range(26): w = gen_word(outer, (i + outer) % 26, (j + outer) % 26, (k + outer) % 26) words.append(w) # print(words) valid_words = [x for x in words if x in word_list] count = len(valid_words) if count > 2: print(valid_words) # print(count) # print(words) # print(valid_words) (_,_,_,prev_count) = best_offset if count > prev_count: best_offset = (i,j,k,count) print(f" updating best match to {i},{j},{k} (words: {valid_words})") print(best_offset)