although there's quite a bit of information about recursion on the web, I haven't found anything that I was able to apply to my problem. I am still very new to programming so please excuse me if my question is rather trivial.
Thanks for helping out :)
This is what I want to end up with:
listVariations(listOfItems, numberOfDigits)
>>> listVariations(['a', 'b', 'c'], 1)
>>> ['a', 'b', 'c']
>>> listVariations(['a', 'b', 'c'], 2)
>>> ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> listVariations(['a', 'b', 'c'], 3)
>>> ['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
but so far I was only able to come up with a function where I need to specify/know the number of digits in advance. This is ugly and wrong:
list = ['a', 'b', 'c']
def listVariations1(list):
variations = []
for i in list:
variations.append(i)
return variations
def listVariations2(list):
variations = []
for i in list:
for j in list:
variations.append(i+j)
return variations
def listVariations3(list):
variations = []
for i in list:
for j in list:
for k in list:
variations.append(i+j+k)
return variations
oneDigitList = listVariations1(list)
twoDigitList = listVariations2(list)
threeDigitList = listVariations3(list)
This is probably very easy, but I couldn't come up with a good way to concatenate the strings when the function calls itself.
Thanks for your effort :)
listas a variable name. It's the constructor for the builtinlistclass and when you do this, you shadow it.