combinatorics

Cartesian product of 2 lists in Haskell

孤者浪人 提交于 2019-11-26 03:25:17
问题 I wish to produce the Cartesian product of 2 lists in Haskell, but I cannot work out how to do it. The cartesian product gives all combinations of the list elements: xs = [1,2,3] ys = [4,5,6] cartProd :: [a] -> [b] -> [(a,b)] cartProd xs ys ==> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] This is not an actual homework question and is not related to any such question, but the way in which this problem is solved may help with one I am stuck on. 回答1: This is very easy with list

Set partitions in Python

允我心安 提交于 2019-11-26 03:25:16
问题 I have an array of [1,2,3] I want to make all the possible combinations using all elements of the array: Result: [[1], [2], [3]] [[1,2], [3]] [[1], [2,3]] [[1,3], [2]] [[1,2,3]] 回答1: Since this nice question has been brought back to life, here's a fresh answer. The problem is solved recursively: If you already have a partition of n-1 elements, how do you use it to partition n elements? Either place the n 'th element in one of the existing subsets, or add it as a new, singleton subset. That's

How to generate all permutations of a list in Python

孤人 提交于 2019-11-26 03:12:48
问题 How do you generate all the permutations of a list in Python, independently of the type of elements in that list? For example: permutations([]) [] permutations([1]) [1] permutations([1, 2]) [1, 2] [2, 1] permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] 回答1: Starting with Python 2.6 (and if you're on Python 3) you have a standard-library tool for this: itertools.permutations. import itertools list(itertools.permutations([1, 2, 3])) If you're using an older

Set partitions in Python

十年热恋 提交于 2019-11-26 01:58:14
I have an array of [1,2,3] I want to make all the possible combinations using all elements of the array: Result: [[1], [2], [3]] [[1,2], [3]] [[1], [2,3]] [[1,3], [2]] [[1,2,3]] Since this nice question has been brought back to life, here's a fresh answer. The problem is solved recursively: If you already have a partition of n-1 elements, how do you use it to partition n elements? Either place the n 'th element in one of the existing subsets, or add it as a new, singleton subset. That's all it takes; no itertools , no sets, no repeated outputs, and a total of just n calls to partition() : def

Permutations - all possible sets of numbers

假如想象 提交于 2019-11-26 01:28:58
问题 I have numbers, from 0 to 8. I would like in result, all possible sets of those numbers, each set should use all numbers, each number can occur only once in a set. I would like to see solution made in PHP that could print out result. Or, at least, I would like some refreshment in theory of combinatorics, as I have long forgotten it. What is the formula to calculate how many permutations will there be? Example sets: 0-1-2-3-4-5-6-7-8 0-1-2-3-4-5-6-8-7 0-1-2-3-4-5-8-6-7 0-1-2-3-4-8-5-6-7 0-1-2

Get all permutations of a PHP array?

南笙酒味 提交于 2019-11-26 00:56:41
问题 Given a PHP array of strings, e.g.: [\'peter\', \'paul\', \'mary\'] How to generate all possible permutations of elements of this array? i.e.: peter-paul-mary peter-mary-paul paul-peter-mary paul-mary-peter mary-peter-paul mary-paul-peter 回答1: function pc_permute($items, $perms = array()) { if (empty($items)) { echo join(' ', $perms) . "<br />"; } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array

Generate all possible combinations of the elements of some vectors (Cartesian product)

六眼飞鱼酱① 提交于 2019-11-25 23:28:33
问题 I would like to generate all the possible combinations of the elements of a given number of vectors. For example, for [1 2] , [1 2] and [4 5] I want to generate the elements: [1 1 4; 1 1 5; 1 2 4; 1 2 5; 2 1 4; 2 1 5; 2 2 4; 2 2 5] The problem is that I don\'t know the number of vectors for which I need to calculate the combinations. There might be 3 as in this case, or there may be 10, and I need a generalization . Can you please help me to this in MATLAB? Is there already a predefined

Generating all Possible Combinations

烂漫一生 提交于 2019-11-25 22:24:41
问题 Given 2 arrays Array1 = {a,b,c...n} and Array2 = {10,20,15....x} how can I generate all possible combination as Strings a(i) b(j) c(k) n(p) where 1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x Such as: a1 b1 c1 .... n1 a1 b1 c1..... n2 ...... ...... a10 b20 c15 nx (last combination) So in all total number of combination = product of elements of array2 = (10 X 20 X 15 X ..X x) Similar to a Cartesian product, in which the second array defines the upper limit for each element in