-2

There is a pool of letters (chosen randomly), and you want to make a word with these letters. I found some codes that can help me with this, but then if the word has for example 2 L's and the pool only 1, I'd like the program to know when this happens.

Pang
  • 9,564
  • 146
  • 81
  • 122
bajotupie
  • 165
  • 1
  • 2
  • 11

1 Answers1

0

If I understand this correctly, you will also need a list of all valid words in whichever language you are using.

Assuming you have this, then one strategy for solving this problem could be to generate a key for every word in the dictionary that is a sorted list of the letters in that word. You could then group all words in the dictionary by these keys.

Then the task of finding out if a valid word can be constructed from a given list of random characters would be easy and fast.

Here is a simple implementation of what I am suggesting:

list_of_all_valid_words = ['this', 'pot', 'is', 'not', 'on', 'top']

def make_key(word):
    return "".join(sorted(word))

lookup_dictionary = {}

for word in list_of_all_valid_words:
    key = make_key(word)
    lookup_dictionary[key] = lookup_dictionary.get(key, set()).union(set([word]))

def words_from_chars(s):
    return list(lookup_dictionary.get(make_key(s), set()))

print words_from_chars('xyz')
print words_from_chars('htsi')
print words_from_chars('otp')

Output:

[]
['this']
['pot', 'top']
Bill
  • 10,323
  • 10
  • 62
  • 85
  • Actually, on re-reading your question, maybe [this](http://stackoverflow.com/questions/8286554/find-anagrams-for-a-list-of-words) is what you were asking for. – Bill Sep 10 '16 at 04:49
  • It is actually something like that, but I got that part already figured out.word=input("pool: ") pool=input("word: ") for i in word[len(word)-1]: if i in pool: print("is") else: print("is not") If for example word= CETA and pool=CTAAE it would print "is". But if word=CETAAAAA it would still print "is" even though there arent enough A's in the pool. Thats my problem. thank you lol I cant understand what I typed. – bajotupie Sep 10 '16 at 05:06
  • sorry it is word=input("word: ") pool=input("pool: ") – bajotupie Sep 10 '16 at 05:13