Question: Write a generator that creates anagrams (words that have the same letters) from a given word. For example, mane - name, lead - deal, ram

Write a generator that creates anagrams (words that have the same letters) from a given word. For example, mane - name, lead - deal, ram - arm - mar are anagrams. Your file should be named generator.py. Please convert your notebook files into python source files!!!

You can use permutations of letters and check if the generated permutation is a word from an English vocabulary. To generate a permutation, you can use the following statement: yield items_permuted items_original + items_permuted where is an index from 1 to the length of the list and items_original and items_permuted are two lists, the first list has items arranged in the original order, and the second list has items that are permuted. The process of permuting a list is to take the first item and place it at different locations inside of the list. For example, let assume that the list is [1, 2, 3], then placing 1 at different locations inside of the list makes the following permutations: and . However, this is not sufficient to generate all permutations: and are missing. We need to permute items in the list before inserting the first item in the list:

for items_permuted in permute(items_original[1:]): for in range(len (items_original)): yield items_permuted tems_original + items_permuted[i:] where permute is a recursive function that should terminate when the list, its parameter, has 1 item.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!