cartesian-product

Cartesian product of javascript object properties

本小妞迷上赌 提交于 2019-12-06 00:59:34
问题 I have an object of the following form (simplified test case below) var test = { shirts: { sizes: ['large', 'medium'] ,colors:['red', 'blue'] } , trousers: { type: ['formal', 'casual'] , pattern: ['plaid', 'stripes'] } }; I want to generate a cartesian product of the properties so that the output is an array of the following form: // desired output [ {shirts:{sizes:'large', color:'red'}, trousers:{type:'formal', pattern:'plaid'}} ,{shirts:{sizes:'large', color:'red'}, trousers:{type:'formal',

Implement Ruby style Cartesian product in Go

女生的网名这么多〃 提交于 2019-12-05 19:19:46
I want to get the Cartesian product of a , b , c , d : a = ['a1'] b = ['b1', 'b2'] c = ['c1', 'c2', 'c3'] d = ['d1'] Here is code in Ruby: e = [b, c, d] print a.product(*e) Output is: [ ["a1", "b1", "c1", "d1"], ["a1", "b1", "c2", "d1"], ["a1", "b1", "c3", "d1"], ["a1", "b2", "c1", "d1"], ["a1", "b2", "c2", "d1"], ["a1", "b2", "c3", "d1"] ] Is there a similar package or function that could do product in Golang? This is just simplified version, in fact, the input data is like [['a1'], ['b1','b2'], ['c1','c2','c3],['d1'],['e1',...],...]. If you need an unknown-at-compile-time set of nested index

MYSQL: Avoiding cartesian product of repeating records when self-joining

独自空忆成欢 提交于 2019-12-05 17:36:48
There are two tables: table A and table B. They have the same columns and the data is practically identical . They both have auto-incremented IDs, the only difference between the two is that they have different IDs for the same records. Among the columns, there is an IDENTIFIER column which is not unique , i.e. there are (very few) records with the same IDENTIFIER in both tables. Now, in order to find a correspondence between the IDs of table A and the IDs of table B, I have to join these two tables (for all purposes it's a self-join) on the IDENTIFIER column, something like: SELECT A.ID, B.ID

How to do Combinatorics on the Fly

匆匆过客 提交于 2019-12-05 17:15:51
I have a very weird problem which has some constrains that make it difficult to solve. I have a list of lists and I want to do the combinations of all items in those lists. Each item has a name and a value. Here is an example: Main List: List 01: Item 01: name:name01, value:value01 Item 02: name:name02, value:value02 List 02: Item 01: name:name03, value:value03 List 03: Item 01: name:name04, value:value04 Item 02: name:name05, value:value05 The end result should look like this: Some List: Item 01: name01:value01, name03:value03, name04:value04 Item 02: name02:value02, name03:value03, name04

Generate Combinations of generic list

岁酱吖の 提交于 2019-12-05 14:00:20
I need to create a list from another list that contains every combination possible. In researching possible solutions I have found many interesting approaches but all seem to generate results based on the count of records provided. I need the combinations to increase to a max threshold. i.e. consider the following array 1,2,3,4,5 I need the results to look similar to (threshold is 3 in this example) 1 1,2 1,2,3 1,2,4 1,2,5 1,3,4 2,3,5... etc In actuality, the data will be IEnumerable. I used a simple int[] to illustrate the desired results. My solution uses a simple recursive algorithm to

Cartesian product in Scheme

纵然是瞬间 提交于 2019-12-05 09:08:46
I've been trying to do a function that returns the Cartesian Product of n sets,in Dr Scheme,the sets are given as a list of lists,I've been stuck at this all day,I would like a few guidelines as where to start. ----LATER EDIT ----- Here is the solution I came up with,I'm sure that it's not by far the most efficent or neat but I'm only studing Scheme for 3 weeks so be easy on me. Here's a concise implementation that is also designed to minimize the size of the resulting structure in memory, by sharing the tails of the component lists. It uses SRFI-1. (define (cartesian-product . lists) (fold

Cartesian Product and Map Combined in Scala

荒凉一梦 提交于 2019-12-05 07:22:26
This is a followup to: Expand a Set of Sets of Strings into Cartesian Product in Scala The idea is you want to take: val sets = Set(Set("a","b","c"), Set("1","2"), Set("S","T")) and get back: Set("a&1&S", "a&1&T", "a&2&S", ..., "c&2&T") A general solution is: def combine[A](f:(A, A) => A)(xs:Iterable[Iterable[A]]) = xs.reduceLeft { (x, y) => x.view.flatMap {a => y.map(f(a, _)) } } used as follows: val expanded = combine{(x:String, y:String) => x + "&" + y}(sets).toSet Theoretically, there should be a way to take input of type Set[Set[A]] and get back a Set[B] . That is, to convert the type

How can I complete this Objective-C implementation of a cartesian product function?

给你一囗甜甜゛ 提交于 2019-12-05 06:26:00
As a follow up to my question here , I am trying to implement the following PHP function in Objective-C, which will generate a cartesian product: function array_cartesian_product($arrays) { $result = array(); $arrays = array_values($arrays); $sizeIn = sizeof($arrays); $size = $sizeIn > 0 ? 1 : 0; foreach ($arrays as $array) $size = $size * sizeof($array); for ($i = 0; $i < $size; $i ++) { $result[$i] = array(); for ($j = 0; $j < $sizeIn; $j ++) array_push($result[$i], current($arrays[$j])); for ($j = ($sizeIn -1); $j >= 0; $j --) { if (next($arrays[$j])) break; elseif (isset ($arrays[$j]))

python all possible pairs of 2 list elements, and getting the index of that pair

北慕城南 提交于 2019-12-04 15:39:16
问题 let's say I have two lists: a = list(1,2,3) b = list(4,5,6) So I can have 9 pairs of these list members: (1,4) (1,5) (1,6) (2,4) (2,5) (2,6) (3,4) (3,5) (3,6) Now, given two list members like above, can I find out the pair's index? Like (1,4) from above would be the 1st pair. 回答1: And to complete the answer and stay in the example: import itertools a = [1, 2, 3] b = [4, 5, 6] c = list(itertools.product(a, b)) idx = c.index((1,4)) But this will be the zero-based list index, so 0 instead of 1.

itertools: Cartesian product of permutations

你离开我真会死。 提交于 2019-12-04 12:06:17
Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended. The expected output is: ((1, 2, 3), (4, 5), (6, 7)) ((1, 2, 3), (4, 5), (7, 6)) ((1, 2, 3), (5, 4), (6