Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a clever way to find all possible combination of these two sets of items?

Tags:

haskell

Let's say I have two kinds of items

data Item1 = A | B | C
data Item2 = D | E | F

And two sets

set1 = [A,B,C]
set2 = [D,E,F]

I would like to find all unique ways of matching the items from two sets, the answer should be (in informal notation):

AD,BE,CF
AD,BF,CE
AE,BD,CF
AE,BF,CD
AF,BD,CE
AF,BE,CD

In other words, I would like some function that accomplish the following:

combine :: [Item1] -> [Item2] -> [[(Item1,Item2)]]
combine = undefined

Note each combination should be a tuple, and each row in the enumeration scheme above should be a list, for example:

[(A,D),(B,E),(C,F)]
like image 393
xiaolingxiao Avatar asked Sep 02 '14 15:09

xiaolingxiao


People also ask

How do you find the number of combinations between two sets?

The formula to calculate the number of possible ways to combine these 2 sets is: 4 elevated 4^4. (for 2 equals sets it could be n^n, being n the size of the sets) And for n equals sets, the formula it should be: n^((n-1)*n) I hope it would help you.

How do you figure out all the possible combinations?

To calculate combinations, we will use the formula nCr = n! / r! * (n - r)!, where n represents the total number of items, and r represents the number of items being chosen at a time.

How do you generate all possible combinations of two lists?

Add a Custom Column to and name it List1. Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!


1 Answers

Use this

import Data.List (sort, permutations)
combine as bs = zipWith zip (repeat as) (sort $ permutations bs)
like image 75
randomusername Avatar answered Sep 28 '22 12:09

randomusername