I need to write a function that receives a string and the number of groups, and then returns all the possible slicing options.
i.e. for func('hello', 3):
[['h', 'e', 'llo'], ['h', 'el', 'lo'], ['h', 'ell', 'o'], ['he', 'll', 'o'], ['he', 'l', 'lo'], ['hel', 'l', 'o']]
How can I do this without using recursion? This is supposed to be possible only using loops and lists.
This is similar to a "divide balls into bins" problem, but in this case it's different because the order of objects never changes, just the slicing.
This is the code I currently have:
def divide_into_two(word):
list_of_divs = []
for i in range(1, len(word)):
list_of_divs.append([word[0:i], word[i:len(word)]])
return list_of_divs
It only divides a word into two groups. I figured we need some kind of inner loop to keep dividing it in case it's more groups, but I'm not sure how to do it.