MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

combination function


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}}










  • Prev by Date: Re: animation
  • Previous by thread: international trade model