This prog takes in an input from the user such as "aeoritre" and outputs any word that can be made from these letters. The dictionary.txt file contains the wordlist.
My question is how do I output just the longest word that the string matches to instead of printing all of them? also if words are tied for longest, output only one of them. Thanks in advance!
from collections import Counter
words = []
def isAnAnagram(word, user):
word_counter = Counter(word)
input_counter = Counter(user)
return all(count <= input_counter[key] for key, count in word_counter.items())
def getAnagrams(user):
lister = [word for word in words if len(word) <= len(user) ]
for item in lister:
if isAnAnagram(item, user):
yield item
with open('Dictionary.txt', 'r') as f:
allwords = f.readlines()
f.close()
for x in allwords:
x = x.rstrip()
words.append(x)
inp = 1
while inp != "99":
inp = input("enter word:")
result = getAnagrams(inp)
print(list(result))
print(len(list(result)))