In: Computer Science
Write code in Python that does the following : An anagram is
when the order of a word can be changed to create a new word
(evil,
live, and vile for example). Using the dictionary provided, find
all of the anagram groups.
Which words contain the most anagrams? How large is this group?
Here is another example:
Alert, alter, and later have 3 in their group. Use the provided
dict.txt as your word list,
solution must also finish in less than a minute.
* I know there is no dict.txt file to use here..
file = open("dict.txt", "r", encoding="utf8")
*looking for a way to sort through the file and group anograms together
*output the groups from largest to smallest (if there is only a
single word in a group, no need to display)
file.close()
To solve this program, I am assuming the dict.txt file contains the words one in each line as specified in the question. A sample dict file screenshot is added here, on which I will be working here-
Here the anagram groups are-
The python code for opening this file and finding all the anagram groups is given below-
from collections import defaultdict
file = open("dict.txt", "r", encoding="utf8")
#Store the file contents in a list
words = []
for line in file.readlines():
words.append(line.strip())
#Close the file
file.close()
#Create default dictionary of list type
d = defaultdict(list)
for word in words:
d[str(sorted(word))].append(word)
#Extract the anagram groups
groups = d.values()
#Remove the group whose length is 1
groups = [group for group in groups if len(group)>1]
#Sort the groups according to their length from largest to smallest
groups = sorted(groups, key = len, reverse = True)
print(groups)
Explanation -
Code Screenshot - for indentation
OUTPUT -
Keep the dict.txt in the same folder as the Python program