def split(d):
yield tuple(d)
if len(d) > 1:
yield from [b for i in range(len(d)-1) for b in split(d[:i]+[d[i]+d[i+1]]+d[i+2:])]
val = 3147
print(set(split(list(map(str, str(val))))))
import itertools
def splitter(L):
for a in itertools.product([0,1], repeat=len(L) - 1):
a = (0,) + a
yield [''.join(map(lambda x: x[0], g))
for _, g in itertools.groupby(zip(L, a), key=lambda x: x[1])]
L = ['3','1','4','7']
for l in splitter(L):
print(l)
def splitter(n):
s = str(n)
for i in range(1, len(s)):
start = s[0:i]
end = s[i:]
yield [int(start), int(end)]
for split in splitter(end):
result = [start]
result.extend(split)
yield list(int(x) for x in result)
较短的递归方法:
输出:
无递归的惰性解决方案:
输出:
您可以使用我的答案here的改编:
代码:
用法:
输出:
相关问题 更多 >
编程相关推荐