ninetynine-1.3.0: Ninety-Nine Haskell Problems
CopyrightCopyright (C) 2021 Yoo Chung
LicenseGPL-3.0-or-later
Maintainerdev@chungyc.org
Safe HaskellSafe-Inferred
LanguageGHC2021

Problems.P27

Description

Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P27.

Synopsis

Documentation

disjointGroups :: [Int] -> [a] -> [[[a]]] Source #

In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3, and 4 persons? Given a list with the sizes of each group and list of items, write a function to return the list of disjoint groups.

Group sizes are larger than zero, and all items in the list to group are distinct. Note that we do not want permutations of the group members. E.g., [1,2] is the same group as [2,1]. However, different groups in the list are considered distinct. E.g., [[1,2],[3,4]] is considered a different list of groups from [[3,4],[1,2]].

You may find more about this combinatorial problem in a good book on discrete mathematics under the term multinomial coefficients.

Examples

>>> let names = ["aldo","beat","carla","david","evi","flip","gary","hugo","ida"]
>>> minimum $ map (map sort) $ disjointGroups [2,3,4] names
[["aldo","beat"],["carla","david","evi"],["flip","gary","hugo","ida"]]
>>> length $ disjointGroups [2,3,4] names
1260
>>> length $ disjointGroups [2,2,5] names
756