August 27, 2024

P26 - Generate the combinations of K K distinct objects chosen from the N N elements of a list.

In how many ways can a committee of 3 be chosen from a group of 12 people?  We all know that there are C(12,3)=220C(12,3)=220 possibilities (C(N,K)C(N,K) denotes the well-known binomial coefficient).  For pure mathematicians, this result may be great.  But we want to really generate all the possibilities.

Example:

scala

scala> combinations(3, List('a, 'b, 'c, 'd, 'e, 'f))
res0: List[List[Symbol]] = List(List('a, 'b, 'c), List('a, 'b, 'd), List('a, 'b, 'e), ...
Be first to comment
Leave a reply