Skip to content

Instantly share code, notes, and snippets.

@kopf kopf/words.md
Created Nov 15, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.