An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram can be rearranged into nag a ram, or the word binary into brainy or the word adobe into abode.
It requires two text(.txt) files.
One file should be meaningful words and another one for input.
Dictionary words list for reference: example2.txt
Input file with words(anagram to be created): example1.txt
Open one jupyter notebook in the same directory where the two files are stored.
Run below parts in each notebook paragraphs.
#Part1
def merged_dict(list_data):
merged = {}
for d in list_data:
for k, v in d.items ():
if k not in merged: merged [k] = []
merged [k].append (v)
return merged
#Part2
f = open(“example2.txt”, “r”)
ref_list = []
merge_dict ={}
for line in f.readlines():
word = line.strip().lower()
sorted_word = “”.join(sorted(list(word)))
ref_dict = {sorted_word:word}
ref_list.append(ref_dict)
ref_list=merged_dict(ref_list)
#Part3
f2 = open(“example1.txt”, “r”)
input_list = []
merge_dict ={}
for line in f2.readlines():
word = line.strip().lower()
sorted_word = “”.join(sorted(list(word)))
input_dict = {sorted_word:word}
input_list.append(input_dict)
input_list = merged_dict(input_list)
#Part4
final_result = []
rdpSet = set(input_list)
namesSet = set(ref_list)
for name in rdpSet.intersection(namesSet):
result = {“input”:input_list[name],”output”:ref_list[name]}
final_result.append(result)
#Part5
final_result = []
merged_inputset = set(input_list)
ref_merged_set = set(ref_list)
for name in merged_inputset.intersection(ref_merged_set):
result = {“input”:input_list[name], “output”:ref_list[name]}
final_result.append(result)#
#OutPut
final_result
Comments
Post a Comment