# Revision history [back]

### Gathering sublists based on functional programming (GatherBy)

Hi, I'm looking at translating a very nice solution to the MrP and MrS puzzle. http://mathematica.stackexchange.com/questions/13898/how-to-improve-this-code-for-solving-the-mr-s-and-mr-p-puzzle

The translation of the problem involves list comprehensions and functional programming. We can generate the tuples of solutions in the form:

possibilities = [ (i,j) for i in range(2,99) for j in range(2,i)]

Then we would like to group these possibilities into lists that have multiple factorizations... In this case informationFilter could be a lambda function for mrP: mrP = lambda a,b : a*b

I am having trouble finding an easy way to do the equivalent of the mathematica function: GatherBy:

GatherBy[possibilities, informationFilter]

So the question is: Given a list of tuples like : [(3, 2), (4, 2), (4, 3), (5, 2), (5, 3), (5, 4), (6, 2), (6, 3), (6, 4), (6, 5)... (98,97)]

How would you gather them into sublists such that their products are the same...

[(4,3),(6,2)] would be one valid sublist.

Many thanks for letting me "think out loud" on this one. Apologies in advance if the answer is trivial.

./pat