cartesian-product

Generating all Possible Combinations

二次信任 提交于 2019-12-12 12:26:21
问题 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

Generate cartesian product in decreasing sum order

好久不见. 提交于 2019-12-12 11:07:59
问题 Given n sorted lists A1, A2, ..., An of integers in decreasing order, is there an algorithm to efficiently generate all elements of their cartesian product in decreasing tuple sum order ? For example, n=3 A1 = [9, 8, 0] A2 = [4, 2] A3 = [5, 1] The expected output would be the cartesian product of A1xA2xA3 in the following order: combination sum 9, 4, 5 18 8, 4, 5 17 9, 2, 5 16 8, 2, 5 15 9, 4, 1 14 8, 4, 1 13 9, 2, 1 12 8, 2, 1 11 0, 4, 5 9 0, 2, 5 7 0, 4, 1 5 0, 2, 1 3 回答1: If the problem

How to sample from Cartesian product without repetition

孤街浪徒 提交于 2019-12-12 09:09:40
问题 I have a list of sets, and I wish to sample n different samples each containing an item from each set. What I do not want is to have it in order, so, for example, I will get all the samples necessarily with the same item from the first set. I also don't want to create all the Cartesian products as that might not be possible in terms of efficiency... Any idea of how to do it? Or even something to approximate this behaviour? Example that does not work: (prod for i, prod in zip(range(n),

RavenDB: How can I properly index a cartesian product in a map-reduce?

百般思念 提交于 2019-12-12 04:25:54
问题 This question is a spin-off of RavenDB: Why do I get null-values for fields in this multi-map/reduce index?, but I realized, the problem was another. Consider my extremely simplified domain, rewritten to a movie rental store scenario for abstraction: public class User { public string Id { get; set; } } public class Movie { public string Id { get; set; } } public class MovieRental { public string Id { get; set; } public string MovieId { get; set; } public string UserId { get; set; } } It's a

Aggregation of cartesian product of two hierachical trees using Java

為{幸葍}努か 提交于 2019-12-12 01:55:20
问题 Need to do aggregation of Cartesian product of two hierarchical tree structures using Java, Please suggest some good methods or API to do this. Tree Structure: Country Tree : Node|Id|ParentId World|1|1 Asia|2|1 Europe|3|1 India|4|2 China|5|2 UK|6|3 Hungary|7|3 Cyprus|8|3 Profit Tree: Node|Id|ParentId Profit|1|1 Revenue|2|1 Expense|3|1 Cartesian product of these two products would give me 24 combinations (8 X 3). I need to aggregate values for each of the combination. For example, I want to

Generating all the values of N nested for loops

…衆ロ難τιáo~ 提交于 2019-12-11 13:58:02
问题 I would like to write a function to do the following, given two arguments to the function int K and int nest_level generate all possible points that result from creating nest_level nested loops where each loop ranges from -K to K . For example if k = 5 and nest_level = 3 the function prints the sequences of numbers that result from the following: for(int i = -k; i <= k; ++i) for(int j = -k; j <= k; ++j) for(int m = -k; m <= k; ++m) print(i, j, k) This should work for any K and nest_level From

Cartesian power (cartesian product with self arbitrary times)

孤街浪徒 提交于 2019-12-11 13:52:51
问题 I have a need in my code to calculate the cartesian product of an array with itself a varying number of times. For example, if my array is [1,2] and I need to fill these values into three slots, the result would be: [1,1,1] [1,1,2] [1,2,1] [1,2,2] [2,1,1] [2,1,2] [2,2,1] [2,2,2] What's the easiest way to do this? 回答1: You are probably looking for permutation with repetition and Ruby's Array from standard library luckily implements this: [1,2].repeated_permutation(3).to_a # [[1, 1, 1], [1, 1,

Prolog creating lists

给你一囗甜甜゛ 提交于 2019-12-11 10:36:57
问题 I'm trying to write a program in Prolog that will take in three lists (all of which are the same length) and return a list of lists. The list of lists that I am returning is a triple that contains elements from the three lists that are being passed in. The first element of the triple is from the first list passed in, the second element of the triple is from the second list, and the third element of the triple is from the third list passed in. What I want to have happen is the list of triples

How to get the cartesian product of two DStream in Spark Streaming with Scala?

穿精又带淫゛_ 提交于 2019-12-11 06:13:34
问题 I have two DStreams. Let A:DStream[X] and B:DStream[Y] . I want to get the cartesian product of them, in other words, a new C:DStream[(X, Y)] containing all the pairs of X and Y values. I know there is a cartesian function for RDDs. I was only able to find this similar question but it's in Java and so does not answer my question. 回答1: The Scala equivalent of the linked question's answer (ignoring Time v3 , which isn't used there) is A.transformWith(B, (rddA: RDD[X], rddB: RDD[Y]) => rddA

Create a list that contains all subsets from an upperbound (but where lst[i] ≤ upper bound[i])

感情迁移 提交于 2019-12-11 04:19:58
问题 I'm trying to build a function that: accepts as an argument a list of positive integers of length n and returns a list of all lists of length n consisting of non-negative integers with the following property: for a list lst it holds that for all indices i, lst[i] ≤ upper bound[i] For example, if the input list was [1, 1, 2] , then the output would be [ [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 0 , 2 ] , [ 0 , 1 , 0 ] , [ 0 , 1 , 1 ] , [ 0 , 1 , 2 ] , [ 1 , 0 , 0 ] , [ 1 , 0 , 1 ] , [ 1 , 0 , 2 ]