I want to make a recursive function called makeCombinations that will take in an input a string and return all possible combinations of its letters as a list. So far I have this:
def delist(l):
'''delist takes in input list l and if it its a nested list returns a new list that de-nests all the lists. Ex: [1,2,[3,4]] -> [1,2,3,4]'''
if(l == []):
return []
else:
if(isinstance(l[0], list)):
return delist(l[0]) + delist(l[1:])
else:
return [l[0]] + delist(l[1:])
def makeCombinations(word, currentString=''):
if(word == ''):
return currentString
else:
use_it = makeCombinations(word[1:], currentString = currentString + word[0])
lose_it = makeCombinations(word[1:], currentString)
return delist([use_it , lose_it])
print(makeCombinations("asmtp"))
When running makeCombinations it returns:
['asmtp', 'asmt', 'asmp', 'asm', 'astp', 'ast', 'asp', 'as', 'amtp', 'amt', 'amp', 'am', 'atp', 'at', 'ap', 'a', 'smtp', 'smt', 'smp', 'sm', 'stp', 'st', 'sp', 's', 'mtp', 'mt', 'mp', 'm', 'tp', 't', 'p', '']
Which is pretty close, however it isn't all, and I am actually looking for very specific words, "a", "at", "am", but also, "spam" which for some reason just wont be outputted. I feel like this is due to the fact I am only taking the right side and not checking the reverse case, but I am not sure how to do this. I can't use loops or itertools.