Set consolidation: Difference between revisions

→‎Iterative: Modify doctest.
(→‎{{header|Python}}: Add Ledrug's recursive solution from talk page.)
(→‎Iterative: Modify doctest.)
Line 61:
[{'A', 'C', 'B', 'D', 'G', 'F', 'I', 'H', 'K'}]
>>> # Confirm order-independence
>>> from copy import deepcopy
>>> import itertools
>>> sets = [{H,I,K}, {A,B}, {C,D}, {D,B}, {F,G,H}, {A,H}]
>>> answer = consolidate(deepcopy(sets))
>>> for perm in itertools.permutations(sets):
assert consolidate(deepcopy(perm)) == answer
 
>>> answer
[{'A', 'C', 'B', 'D', 'G', 'F', 'I', 'H', 'K'}]
>>> len(list(itertools.permutations(sets)))
720
Line 83 ⟶ 86:
return [s for s in setlist if s]
</lang>
 
===Recursive===
<lang python>def conso(s):
Anonymous user