cartesian-product

Algorithm to get Cartesian product

五迷三道 提交于 2019-12-20 05:23:33
问题 I have an array like [0,2,3,0,1] as input , and I need to find Cartesian product of {0}x{0,1,2}x{0,1,2,3}x{0}x{0,1} , more precisely I need to have output as following. Input: [0, 2, 3, 0, 1] Output: [0, 0, 0, 0, 0] [0, 0, 0, 0, 1] [0, 0, 1, 0, 0] [0, 0, 1, 0, 1] [0, 0, 2, 0, 0] [0, 0, 2, 0, 1] [0, 0, 3, 0, 0] [0, 0, 3, 0, 1] [0, 1, 0, 0, 0] [0, 1, 0, 0, 1] [0, 1, 1, 0, 0] [0, 1, 1, 0, 1] [0, 1, 2, 0, 0] [0, 1, 2, 0, 1] [0, 1, 3, 0, 0] [0, 1, 3, 0, 1] [0, 2, 0, 0, 0] [0, 2, 0, 0, 1] [0, 2, 1,

Logic to select a specific set from Cartesian set

二次信任 提交于 2019-12-20 04:12:10
问题 I'm making a password brute forcing tool as a learning exercise, and I want it to be resumable. So, what I want is to be able to say, this is the set of possible characters, if I computed the Cartesian set of every possible combination of this set up to length n, what is the set at point x? However, I want to do this without computing the entire set. I've seen similar logic in one place online but I was unable to generalise this to fit. Any help would be fantastic, thanks! I'm fluent in C# if

Logic to select a specific set from Cartesian set

雨燕双飞 提交于 2019-12-20 04:12:04
问题 I'm making a password brute forcing tool as a learning exercise, and I want it to be resumable. So, what I want is to be able to say, this is the set of possible characters, if I computed the Cartesian set of every possible combination of this set up to length n, what is the set at point x? However, I want to do this without computing the entire set. I've seen similar logic in one place online but I was unable to generalise this to fit. Any help would be fantastic, thanks! I'm fluent in C# if

Cartesian product of string[] with itself without duplicate/clone directly in C#

牧云@^-^@ 提交于 2019-12-20 02:28:23
问题 Please excuse my lack of better terminology. I have a string[] and I'd like to get the product of a catesian join with itself for later to put in a dictionary. However, I'd like all possible combinations except when there is a clone/duplicate key. string[] array1 = { "aa", "bb", "cc" }; string[][] arrayOfArrays = array1.SelectMany(left => array1, (left, right) => new string[] { left, right }).ToArray(); Will obtain a string[][] from all possible combinations yielding as each element: aa aa aa

Cartesian product in clojure

孤街浪徒 提交于 2019-12-18 20:14:28
问题 I'm trying to implement a method that will take a list of lists and return a the cartesian product of these lists. Here's what I have so far: (defn cart ([] '()) ([l1] (map list l1)) ([l1 l2] (map (fn f[x] (map (fn g [y] (list x y)) l2)) l1) ) ) (defn cartesian-product [& lists] (reduce cart lists) ) ;test cases (println (cartesian-product '(a b) '(c d))) ; ((a c) (a d) (b c) (b d)) (println (cartesian-product ())) ;() (println (cartesian-product '(0 1))) ; ((0) (1)) (println (cartesian

In Perl, how can I iterate over the Cartesian product of multiple sets?

对着背影说爱祢 提交于 2019-12-18 08:49:51
问题 Given x number of arrays, each with a possibly different number of elements, how can I iterate through all combinations where I select one item from each array? Example: [ ] [ ] [ ] foo cat 1 bar dog 2 baz 3 4 Returns [foo] [cat] [ 1 ] [foo] [cat] [ 2 ] ... [baz] [dog] [ 4 ] I'm doing this in Perl, btw. 回答1: My Set::CrossProduct module does exactly what you want. Note that you aren't really looking for permutations, which is the ordering of the elements in a set. You're looking for the cross

Creating Combinations in JavaScript

时光怂恿深爱的人放手 提交于 2019-12-18 07:02:05
问题 Lets say I have several sets of options in Javascript var color = ["red", "blue", "green","yellow"]; var size = ["small", "medium", "large"]; var weight = ["heavy", "light"]; what is an efficient algorithm to get all the combinations of these options in an array that looks like this ["red and small and heavy", "red and small and light", "red and medium and heavy" ...] Here's the caveat though This function must be able to take any number of sets of options I have a feeling that the proper way

Create a type list combination of types in C++

眉间皱痕 提交于 2019-12-18 05:44:18
问题 Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or C. For a N=2 case, this would be std::tuple<A,A> std::tuple<A,B> std::tuple<A,C> std::tuple<B,A> std::tuple<B,B> std::tuple<B,C> std::tuple<C,A> std::tuple<C,B> std::tuple<C,C> The idea is to create a tuple which holds a container for all those types,

Cartesian product in MATLAB

荒凉一梦 提交于 2019-12-18 03:12:13
问题 Here is the simplified version of the problem I have. Suppose I have a vector p=[1 5 10] and another one q=[.75 .85 .95]. And I want to come up with the following matrix: res=[1, .75;1, .85; 1, .95; 5, .75; 5, .85; 5, .95; 10, .75; 10, .85; 10, .95]. This is also known as the Cartesian Product. How can I do that? Many thanks 回答1: Here's one way: [X,Y] = meshgrid(p,q); result = [X(:) Y(:)]; The output is: result = 1.0000 0.7500 1.0000 0.8500 1.0000 0.9500 5.0000 0.7500 5.0000 0.8500 5.0000 0

Permutations of parameters (i.e. Cartesian product) into a multi-dimensional array

断了今生、忘了曾经 提交于 2019-12-17 22:00:57
问题 I am interested in calculating a function on a bunch of permutations of parameter values. I want to keep it generic to N dimensions, but let me write it out in 3 dimensions to start with. Generating the permutations is easy enough with meshgrid , but I can't figure out how to reshape the resulting array back to the multidimensions? Here is a starting point: %These are the 3 variations of parameters, with some values. params1 = [100, 200, 300];%Picking these so it is easy to correlate to the