cartesian-product

Postgresql: Insert the cartesian product of two or more sets

自古美人都是妖i 提交于 2019-12-04 10:17:07
as definition: The cartesian product of two sets is the set of all possible pairs of these sets, so {A,B} x {a,b} = {(A,a),(A,b),(B,a),(B,b)}. Now i want to insert such a cartesian product into a database table (each pair as a row). It is intended to fill the table with default values for each pair, so the data, i.e. the two sets, are not present in the database at this point. Any idea how to achieve this with postgresql? EDIT : With the help of Grzegorz Szpetkowski's answer I was able to produce a query that does what I want to achieve, but it really isn't the prettiest one. Suppose I want to

Cartesian product of javascript object properties

半世苍凉 提交于 2019-12-04 06:06:37
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', pattern:'stripes'}} ,{shirts:{sizes:'large', color:'red'}, trousers:{type:'casual', pattern:'plaid'}}

Stream of cartesian product of other streams, each element as a List?

↘锁芯ラ 提交于 2019-12-04 05:46:29
How can I implement a function using Java 8 to take some number of streams, and produce a stream in which each element is a list consisting of one member of the Cartesian product of the streams? I've looked at this question -- that question uses an aggregator that is a BinaryOperator (taking two items of like type and producing an item of the same type). I'd like the items in the end result to be List s rather than the types of the elements in the input streams. Concretely, supposing my desired function is called product , the following: Stream<List<String>> result = product( Stream.of("A", "B

C# advanced permutation scenario

喜夏-厌秋 提交于 2019-12-04 04:03:34
问题 I am trying to figure how how to find all the combinations given the following information: I start with a JSON dataset: var choices = { 1: {'Q': 100, 'R': 150, 'W' : 250, 'T', 30}, 2: {'Q': 90, 'R': 130, 'W' : 225, 'T', 28}, 3: {'Q': 80, 'R': 110, 'W' : 210, 'T', 25}, 4: {'Q': 70, 'R': 90, 'W' : 180, 'T', 22}, 5: {'Q': 60, 'R': 70, 'W' : 150, 'T', 18}, 6: {'Q': 50, 'R': 50, 'W' : 110, 'T', 15}, 7: {'Q': 40, 'R': 30, 'W' : 80, 'T', 11}, 8: {'Q': 30, 'R': 25, 'W' : 50, 'T', 8}, 9: {'Q': 20, 'R

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

橙三吉。 提交于 2019-12-03 09:44:09
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. 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. One way to do this: Find the first element of the pair your are looking for in the first list: p = (1, 4) i

Clear explanation of the “theta join” in relational algebra?

半城伤御伤魂 提交于 2019-12-03 09:28:59
问题 I'm looking for a clear, basic explanation of the concept of theta join in relational algebra and perhaps an example (using SQL perhaps) to illustrate its usage. If I understand it correctly, the theta join is a natural join with a condition added in. So, whereas the natural join enforces equality between attributes of the same name (and removes the duplicate?), the theta join does the same thing but adds in a condition. Do I have this right? Any clear explanation, in simple terms (for a non

Why do the results of this MySQL query get multiplied by each other?

最后都变了- 提交于 2019-12-03 08:44:06
SELECT user_id, SUM(COALESCE(point_points, 0)) AS total_points, SUM( CASE WHEN point_date > '$this_month' THEN point_points ELSE 0 END) AS month_points, COUNT(DISTINCT c_id) AS num_comments, COUNT(DISTINCT rant_id) AS live_submissions FROM users LEFT JOIN points ON users.user_id = points.point_userid LEFT JOIN comments ON ( c_userid = user_id ) LEFT JOIN rants ON ( rant_poster = user_id AND rant_status = 1 ) WHERE user_id = $id GROUP BY user_id Basically live_submissions and num_comments variable display proper results, while the total_points and month_points display a product of month_points

How to create cartesian product over arbitrary groups of numbers in Java?

 ̄綄美尐妖づ 提交于 2019-12-03 06:52:32
Let's say I have 2 groups of numbers: {1, 2, 3}, {4, 5} I'd like to create an algorithm (in Java) that outputs the following 6 combinations: 1,4 1,5 2,4 2,5 3,4 3,5 There can be an arbitrary number of groups and an arbitrary number of members within each group. So in the above example, there are 2 groups with the first group having 3 members and the second group having 2 members. Another example is the following (3 groups, 3 members in first groups and 2 members in second and third groups): {1, 2, 3}, {4, 5}, {6, 7} Which would yield the following 12 combinations: 1,4,6 1,4,7 1,5,6 1,5,7 2,4,6

Generate the Cartesian Product of 2 vector<string>s In-Place?

删除回忆录丶 提交于 2019-12-02 11:48:33
问题 If I want to get the Cartesian Product of these two vector<string> s: vector<string> final{"a","b","c"}; vector<string> temp{"1","2"}; But I want to put the result in final , such that final would contain: a1 a2 b1 b2 c1 c2 I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final is not important. 回答1: You may try the following approach #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string>

Linear time algorithm to compute cartesian product

心已入冬 提交于 2019-12-02 06:12:37
问题 I was asked in an interview to come up with a solution with linear time for cartesian product. I did the iterative manner O(mn) and a recursive solution also which is also O(mn). But I could not reduce the complexity further. Does anyone have ideas on how this complexity can be improved? Also can anyone suggest an efficient recursive approach? 回答1: There are mn results; the minimum work you have to do is write each result to the output. So you cannot do better than O(mn) . 回答2: The question