Skip to content

Instantly share code, notes, and snippets.

@kopf
Created November 15, 2016 09:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kopf/b08ba88e228e1e3377999a06149ab510 to your computer and use it in GitHub Desktop.
find all words, at least 3 letters long, for which all permutations form valid words

Results

Using a wordlist of ~350k words from https://github.com/dwyl/english-words/blob/master/words.txt?raw=true

aewalto@laptop:~/tmp $ python words.py wordlist.txt 
Processing 354526 words...
aaa
app
iii
oho
tea
eat
eer
eta
hoo
oooo
ppa
ate
tae
aet
ere
ooh
pap
ree
xxx
mmmm

words.py

import multiprocessing
import sys
import itertools


MIN_LENGTH = 3
CHUNK_SIZE = 10000
with open(sys.argv[-1], 'r') as f:
    data = f.read()
DICTIONARY = [word.strip().lower() for word in data.split('\n') if len(word) >= MIN_LENGTH]


def is_valid(word):
    for permutation in itertools.permutations(word):
        permutation = ''.join(permutation)
        if permutation not in DICTIONARY:
            return False
    return True


def run(chunk):
    for word in chunk:
        if is_valid(word):
            print(word)


if __name__ == '__main__':
    print("Processing {} words...".format(len(DICTIONARY)))
    p = multiprocessing.Pool(8)
    chunks = [DICTIONARY[i::int(len(DICTIONARY)/CHUNK_SIZE)] for i in range(int(len(DICTIONARY)/CHUNK_SIZE))]
    p.map(run, chunks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment