0

Given an input of scrambled word, and I have a file of English words. I want to find all possible words that can be formed from the scrambled word.

from itertools import permutations
def unscramble(scrambled_word):
    length=len(scrambled_word)
    unscrambled_word=[]
    
    for r in range(length):
        if r >= 2:
            permutation_object=permutations(scrambled_word, r)
            unscrambled_list=[''.join(permutation) for permutation in permutation_object]
            unscrambled_word.extend(unscrambled_list)
    return unscrambled_word

def english_words():
    with open("english_words.txt") as file_object:
        content=file_object.read()
        return content

unscrambled_word=unscramble("nidswow")
english_words=english_words()

for word in unscrambled_word:
        if word in english_words:
            print(word)
  • 1
    Sort the letters in all the words in your list. Make a dict with the sorted letters as a key, and the list of matching words as the value. Then, you can sort the incoming word and do an exact lookup. – Tim Roberts Jan 19 '22 at 07:53

2 Answers2

1

As suggested by Tim's comment, one way to solve this would be using a dictionary of the English words and having the sorted words as keys. To populate the dictionary, just iterate over each English word, sort it to get the key, and append the word to the dict value, which can be an empty list to begin with (defaultdict is perfect for this). For this example, I got the English words from here.

from collections import defaultdict

words_dict = defaultdict(list)

with open('words_alpha.txt', 'r') as f:
    for line in f:
        word = line.strip()
        k = ''.join(sorted(word))
        words_dict[k].append(word)

The dictionary now looks something like this:

defaultdict(list,
            {'a': ['a'],
             'aa': ['aa'],
             'aaa': ['aaa'],
             'aah': ['aah', 'aha'],
             'aadeh': ['aahed', 'ahead'],
             'aaghin': ['aahing'],
             'aahs': ['aahs', 'asha'],
             'aal': ['aal', 'ala'],
             'aaiil': ['aalii'],
             'aaiils': ['aaliis', 'sialia'],
             'aals': ['aals', 'alas', 'lasa', 'sala'],
             'aam': ['aam', 'ama'],
             'aain': ['aani'],
             ...})

To get the matching words out of a scrambled one, you can use a similar approach. First, you generate a key by sorting the scrambled word, then you use the key to get the list of matching words from the dictionary.

scrumbled = 'lbae'
k = ''.join(sorted(scrumbled)) 
matches = words_dict[k]
print(matches)

Output:

['abel', 'able', 'albe', 'bael', 'bale', 'beal', 'bela', 'blae', 'blea']
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

I want to find all common words in the same key of 2 dictionaries. The Key has a list of words as a value, and the key is the length of the items in the value list.

from collections import defaultdict from itertools import permutations

def unscramble(scrambled_word):
    length=len(scrambled_word)
    unscrambled_word=[]
    unscramble_dict=defaultdict(list)

    for r in range(2,length+1):
        permutation_object=permutations(scrambled_word, r)
        unscrambled_list=[''.join(permutation) for permutation in permutation_object]
        unscramble_dict[r].append(unscrambled_list)
    return unscramble_dict


def english_words(scrambled_word):
    length=len(scrambled_word)
    words_list=[]
    words_dict=defaultdict(list)
    with open("english_words.txt") as file_object:
        for line in file_object:
            word=line.strip()
            for r in range(2,length+1):
                if len(word)==r:
                    words_dict[len(word)].append(word)
    return words_dict



def possible_words():
    pass


print(unscramble("sodwind"))
print(english_words("sodwind"))