Skip to content

Instantly share code, notes, and snippets.

@hakanai
Last active March 19, 2024 00:16
Show Gist options
  • Save hakanai/3d28794ad6a45b2c698d5e991136f97b to your computer and use it in GitHub Desktop.
Save hakanai/3d28794ad6a45b2c698d5e991136f97b to your computer and use it in GitHub Desktop.
Word combination finder

Started with https://gist.github.com/gubatron/65a153478149118908c6

  • Made it work (original was probably Python 2?)
  • Significantly tidied the code
  • Performance: Removed redundant sorting
  • Performance: Made input permutations unique so identical combinations weren't tried more than once

Still very slow for inputs 9+ letters long. Roughly exponential - takes around 1s for an 8 letter word, 10-11x longer for each additional letter.

import os
from itertools import groupby, permutations
from time import time
def clean_word(word):
return word.strip().lower()
def load_words():
words = {}
f = open('words.txt')
f.seek(0, os.SEEK_END)
end = f.tell()
f.seek(0)
loaded = 0
while f.tell() < end:
word = clean_word(f.readline())
words[word] = ''
loaded += 1
if loaded % 100 == 0:
print("\r%d words loaded" % loaded, end = '')
print("\rFinished loading dictionary - %d words" % loaded)
return words
def find_words(letters, words):
letters = clean_word(letters)
MIN_WORD_LENGTH = 3
key_func = lambda x: ''.join(x)
return [
perm for comb_size in range(MIN_WORD_LENGTH, len(letters) + 1)
for perm in [k for k, _ in groupby(sorted(permutations(letters, comb_size), key=key_func), key=key_func)]
if perm in words
]
words = load_words()
while True:
letters = input("ENTER LETTERS TO FIND WORDS (Press Ctrl-D to end): ")
print("Generating valid combinations...")
t0 = time()
found = find_words(letters, words)
t1 = time()
total_time = t1 - t0
WORDS_PER_LINE = 4
word_count = 0
for f in found:
print(f, " ", end = '')
word_count += 1
if word_count == WORDS_PER_LINE:
print("")
word_count = 0
print("")
print("Finished in %s s" % total_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment