Skip to main content

Create Anagram using Python code

Title picture

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

Image for post
example2.txt
Image for post
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

Image for post
Image for post
Image for post
Output

Comments

Popular posts from this blog

What is AI and where it can use ?

     Artificial intelligence (AI) is a rapidly growing field that has the potential to revolutionise a wide range of industries. At its core, AI is the simulation of human intelligence in machines that are programmed to think and learn like humans. One of the most common and well-known use cases for AI is in the field of image and speech recognition. AI algorithms can be trained to identify and classify images and speech with a high degree of accuracy, making them useful in applications such as facial recognition and voice-controlled assistants. Another important use case for AI is in the field of natural language processing (NLP). NLP algorithms can be used to analyse and understand human language, making them useful for tasks such as language translation, sentiment analysis, and text summarisation. AI is also being used to improve the efficiency and accuracy of decision making in a wide range of industries, including finance, healthcare, and transportation. In finance, AI algorithms

What is ChatGPT ?

  What is ChatGPT ? ChatGPT is a large language model developed by OpenAI. It is trained on a diverse range of internet text and is able to generate human-like text in a variety of styles and formats. Some of the major advantages of using ChatGPT include its ability to generate high-quality and coherent text, as well as its ability to complete text-based tasks such as answering questions, summarising articles, and writing creative fiction. Additionally, ChatGPT can be fine-tuned on specific tasks or domains, making it a versatile tool for a wide range of applications. Some additional advantages of using ChatGPT include: Generating human-like text: ChatGPT's training data includes a wide range of internet text, which allows it to generate text that is difficult to distinguish from text written by humans. Handling long-form text: ChatGPT can generate and complete long-form text, such as articles or stories, which makes it useful for tasks such as content creation and summarisation. H

What is Cloud Computing, How to learn it ?

Cloud computing is the delivery of computing services—including servers, storage, databases, networking, software, analytics, and intelligence—over the Internet (“the cloud”) to offer faster innovation, flexible resources, and economies of scale. Cloud computing allows users to access technology-enabled services from the Internet, instead of building and maintaining infrastructure in-house. Cloud computing has revolutionised the way businesses and individuals access and utilize technology. By leveraging the power of the Internet, cloud computing allows for the delivery of a wide range of computing services, including servers, storage, databases, networking, software, analytics, and intelligence. One of the key benefits of cloud computing is the ability to offer faster innovation. Rather than having to build and maintain expensive infrastructure in-house, businesses and individuals can access the latest technology and services via the Internet. This allows them to quickly and easily imp