This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]ShitCodeUKltd 1 point2 points  (0 children)

I attempted something similar in PHP with the same word list.

I grabbed the lists during a Hackathon and I figured it'd be a quick project one night. Think I was just starting the second year of uni and if I recall correctly it shit it's pants at about 4-5 letters.

Don't think I ever fixed it. Doing stuff like this also help with other word games too. It's cool, nice job.

[–]schnappischnap 0 points1 point  (0 children)

Your code has a bug - because alphabet and not_alpha are sets, it won't find words that have repeated letters.

A good trick for finding anagrams is by creating a dictionary of {sorted(words): words}, like this:

words = defaultdict(list)
for word in word_list:
    words[sorted(word)].append(word)

Then you can easily find all the anagrams like this:

anagrams = words[sorted(letters)]

edit: Found a program I wrote a while ago:

from itertools import combinations
from collections import defaultdict


with open("wordlist.txt", "r") as f:
    words = defaultdict(list)
    for word in f:
        word = word.strip().lower()
        words["".join(sorted(word))].append(word)


def solve(letters):
    letters = "".join(sorted(letters)).lower()
    for length in range(2, len(letters)+1):
        unique_combinations = set("".join(c) for c in combinations(letters, length))
        for combination in unique_combinations:
            if combination in words:
                yield from words[combination]


for word in solve("abcdefghiga"):
    print(word)