Tag Info

New answers tagged

1

My first attempt was to port Leonid's unsortedComplement from Removing elements from a list which appear in another list into Rojo's partitions code from Partition a set into subsets of size $k$ (to allow repeated elments) but as LLlAMnYP commented that was a wasteful choice. Starting again from scratch, though based on Rojo's function: foo[a_List] := ...


4

My take: genIdx[n_?EvenQ] := Flatten@With[{r = Range@n}, Fold[With[{l1 = #, l2 = #2}, Flatten[Map[With[{la = #, c = Complement[r, #]}, Join[la, c[[#]]] & /@ l2] &, l1], 1]] &, Subsets[Range@#, {2}, # - 1] & /@ Range[#, 2, -2] &@n]]; xformLst[lst_, idx_] := If[(Length@lst*(Length@lst - 1)!!) == Length@idx, ...


4

Here's a completely different and non-bruteforcing approach, so I'm adding it as a separate answer. Helper function: help[list_] := Join[{First@list}, #] & /@ Rest@list Main function: iterate[list_List /; Length@list < 3] := {{list}} iterate[list_List /; Length@list > 3] := Module[ {sublists = iterate /@ (Delete[list, {{1}, {#}}] ...


1

Possibly similar to the solution in the comments. list = {x1, x1, x3, x4}; Map[list[[#]] &, DeleteDuplicates[ Map[Sort, Partition[#, 2] & /@ Permutations[Range[Length@list]], 2]], {2}] {{{x1, x1}, {x3, x4}}, {{x1, x3}, {x1, x4}}, {{x1, x4}, {x1, x3}}} Efficiency decays fast (as length!). Treating repeated identical elements as distinct ...


4

Not efficient: << Combinatorica` list = {a, a, c, d}; idx[n_] := Select[SetPartitions[n], Union[Length /@ #] == {2} &]; confs[set_] := Map[set [[#]] &, idx[Length@set], {2}] confs@list (* {{{a, a}, {c, d}}, {{a, d}, {a, c}}, {{a, c}, {a, d}}} *)



Top 50 recent answers are included