Unfortunately I cannot help just now with a recursive function, but given that a higher count of letters/characters can easily explode into billions of potential combinations if not filtered during creation I have a quirky alternative by iterating over the known words. Those have to be in memory anyway.
[EDIT] Removed the sorting as it does not really provide any benefit, fixed an issue where I falsely set to true on iteration
# Some letters, separated by space
letters = 'c a t b'
# letters = 't t a c b'
# # Assuming a word per line, this is the code to read it
# with open("words_on_file.txt", "r") as words:
# words_to_look_for = [x.strip() for x in words]
# print(words_to_look_for)
# Alternative for quick test
known_words = [
'cat',
'bat',
'a',
'cab',
'superman',
'ac',
'act',
'grumpycat',
'zoo',
'tab'
]
# Create a list of chars by splitting
list_letters = letters.split(" ")
for word in known_words:
# Create a list of chars
list_word = list(word)
if len(list_word) > len(list_letters):
# We cannot have longer words than we have count of letters
# print(word, "too long, skipping")
continue
# Now iterate over the chars in the word and see if we have
# enough chars/letters
temp_letters = list_letters[:]
# This was originally False as default, but if we iterate over each
# letter of the word and succeed we have a match
found = True
for char in list_word:
# print(char)
if char in temp_letters:
# Remove char so it cannot match again
# list.remove() takes only the first found
temp_letters.remove(char)
else:
# print(char, "not available")
found = False
break
if found is True:
print(word)
You could copy&paste a product function from the itertools documentation and use the code provided by ExtinctSpecie, it has no further dependencies, however I found without tweaking it returns all potential options including duplications of characters which I did not immediately understand.
def product(*args, repeat=1):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)