combination function
- To: mathgroup at yoda.physics.unc.edu, serhody at rodan.acs.syr.edu
- Subject: combination function
- From: gaylord at ux1.cso.uiuc.edu
- Date: Fri, 28 Feb 1992 06:54:10 -0600
yet another way: subsets[x_List] := Sort[Flatten/@Distribute[{{},{#}}&/@x, List]] combo[x_List, r_Integer] := Cases[subsets[x], Table[_, {r}]] combo[{a, b, c, d}, 3] {{a, b, c}, {a, b, d}, {a, c, d}, {b, c, d}} combo[{a, b, c, d}, 2] {{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}} ================================================ using the subset function taken from somewhere (possibly an issue of Mathematica J., perhaps a one-liner therein?) subsets[x_List] := Sort[Flatten/@Distribute[{{},{#}}&/@x, List]] combination[x_List, r_Integer] := Select[subsets[x], Length[#] == r&] combination[{a, b, c, d}, 3] {{a, b, c}, {a, b, d}, {a, c, d}, {b, c, d}} combination[{a, b, c, d}, 2] {{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}