作者:
sharkbay (Shark Bay)
2021-01-22 06:27:43目前已知這個方法能滿足這個題目, 但是空間或運算量太大, 想請問有沒有別的做法?
import itertools
powerset = lambda iterable: itertools.chain.from_iterable(
itertools.combinations(list(iterable), r)
for r in range(len(list(iterable)) + 1))
flatten = lambda list2d: [item for sublist in list2d for item in sublist]
x = list("abcd")
xx = [list(val) for val in list(powerset(x)) if 0 != len(val)]
xxx = [list(val) for val in list(powerset(xx)) if 0 != len(val)]
xxxx = [list(val) for val in xxx if x == list(sorted(flatten(val)))]
xxxx =
[[['a', 'b', 'c', 'd']],
[['a'], ['b', 'c', 'd']],
[['b'], ['a', 'c', 'd']],
[['c'], ['a', 'b', 'd']],
[['d'], ['a', 'b', 'c']],
[['a', 'b'], ['c', 'd']],
[['a', 'c'], ['b', 'd']],
[['a', 'd'], ['b', 'c']],
[['a'], ['b'], ['c', 'd']],
[['a'], ['c'], ['b', 'd']],
[['a'], ['d'], ['b', 'c']],
[['b'], ['c'], ['a', 'd']],
[['b'], ['d'], ['a', 'c']],
[['c'], ['d'], ['a', 'b']],
[['a'], ['b'], ['c'], ['d']]]