http://qs1969.pair.com?node_id=584963

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have a list of peoples names:

my @people = ("Peter", "Paul", "Jane", "Mary", "Fred");

I want to arrange them into teams:

my @example_teams = (["Peter", "Mary"], ["Paul"], ["Jane", "Fred"]);

In fact, I want to get all possible teams:

my @all_teams = ( [["Peter"], ["Mary"], ["Paul"], ["Jane"], ["Fred"]], [["Peter", "Paul"], ["Mary"], ["Jane"], ["Fred"]], . . . [["Peter", "Mary", "Paul", "Jane", "Fred"]] );

But not by iterating over them and storing them in a table, because there isn't enough memory.

Instead I want a function that takes a scalar number and returns a given grouping and visa-versa:

lookupteam(\@people, \@example_team); # returns 42 constuctteam(\@people, 42); # returns [["Peter", "Mary"], ["Paul"], ["Jane", "Fred"]]

I understand this has something to do with algorithms for perumtations, combinatorics, stirling numbers of the second kind, etc, etc.

Can anyone give me some pointers on where to look, or what the algorithm is called? Or even write a quick version of lookupteam and constructteam?

Thanks, Andrew.