An anagram solver program in Python can be created using the following steps:
Here's an example code:
import itertools
import re
# Load dictionary file
with open('dictionary.txt', 'r') as file:
dictionary = [word.strip() for word in file.readlines()]
# Get input word/phrase from user
input_str = input('Enter a word or phrase: ')
# Remove non-alphabetic characters and convert to lowercase
input_str = re.sub(r'[^a-zA-Z]', '', input_str).lower()
# Get all permutations of letters
perms = [''.join(perm) for perm in itertools.permutations(input_str)]
# Check each permutation against dictionary
anagrams = []
for word in perms:
if word in dictionary:
anagrams.append(word)
# Print list of anagrams
print(anagrams)
Note: The dictionary file used in this example must contain one word per line.
Asked: 2023-01-12 11:00:00 +0000
Seen: 18 times
Last updated: Apr 12 '21