如何找到所有可能的方法将字符串划分为n个切片而无需递归?
我需要编写一个接收字符串和组数的函数,然后返回所有可能的切片选项。
即对于func('hello', 3):
[['h', 'e', 'llo'], ['h', 'el', 'lo'], ['h', 'ell', 'o'], ['he', 'll', 'o'], ['he', 'l', 'lo'], ['hel', 'l', 'o']]
如何在不使用递归的情况下做到这一点?这应该只能使用循环和列表才能实现。
这类似于“将球分成垃圾箱”的问题,但在这种情况下它是不同的,因为对象的顺序永远不会改变,只是切片。
这是我目前拥有的代码:
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
它只将一个词分为两组。我想我们需要某种内部循环来继续划分它,以防它有更多的组,但我不知道该怎么做。
回答
使用itertools.combinations挑哪里绳剪断指数:
from itertools import combinations
def func(s, n):
return [[s[i:j] for i, j in zip([None, *cuts], [*cuts, None])]
for cuts in combinations(range(1, len(s)), n-1)]
结果func('hello', 3):
[['h', 'e', 'llo'], ['h', 'el', 'lo'], ['h', 'ell', 'o'], ['he', 'l', 'lo'], ['he', 'll', 'o'], ['hel', 'l', 'o']]
另一种解决方案,从所有单片分割(即,只是整个字符串)开始,然后计算两片分割,然后是三片分割等。通过始终将最后一个切片分成两部分来完成,以所有允许的方式所需的剩余分割数:
def func(s, n):
divs = [[s]]
for i in range(n-1):
divs = [[*keep, last[:j], last[j:]]
for *keep, last in divs
for j in range(1, len(last)-n+i+2)]
return divs