combinations

Loop i and j, where i != j

流过昼夜 提交于 2019-12-24 12:15:08
问题 For example I need to get all combinations of 2-values array with numbers {0, 1, 2} where this two numbers are not the same. I get 0 1 0 2 1 0 1 2 2 0 2 1 and I ignore 0 0 1 1 2 2 Now I use for (int i= 0; i < L ; i++) { for (int j = 0; j < L; j++) { if (i!= j) { but it is very slow? Any solution for this? L will be > 4000. What am I doing is finding every combinations of splitting matrix to 4 sub-matrices example: 3 | 0 2 -8 -8 5 | 3 2 2 3 2 | 5 2 1 4 ------------------- 3 4 -1 | 4 2 -3 6 2 |

python get groups of combinations that each member appear only once

一个人想着一个人 提交于 2019-12-24 11:53:36
问题 I'm trying to get groups of permutations/combinations (r=2) that each member appear only once I used python 'combinations' package to have the combinations. For example: the members are: a,b,c,d. The combinations are: [a,b],[a,c],[a,d],[b,c],[b,d]... My desired output is: [ {[a,b],[c,d]},{[a,c],[b,d]},{[a,d],[b,c]}...] I would like to know what is the terminology for that case and if there is already implementation for that. Thanks. 回答1: Here's a way to do it: from itertools import

How can I properly return ArrayList<Object> from recursive method without repeated values?

家住魔仙堡 提交于 2019-12-24 11:43:14
问题 I need a recursive solution which returns any combination of n subset from k set (in mathematical sense). I have an ArrayList and I want return any possible n-size subsets from recursive method. Order doesn't matter. So if I have set of employees {Jim, Tom, Ann, John} and want 2 of them I should get: {Jim Tom}{Jim Ann}{Jim John}{Tom Ann}{Tom John}{Ann John} I found this https://stackoverflow.com/a/16256122/10929764 but it only prints out result. I modified it a bit to add any combination to

How to calculate the number of all possible combinations for a range of numbers from 1 to N?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 11:32:25
问题 Other than doing this: from itertools import combinations def brute_force(x): for l in range (1,len(x)+1): for f in list(combinations(range(0,len(x)),l)): yield f x = range(1,18) len(list(brute_force(x))) [out]: 131071 How could I mathematically calculate the number of all possible combinations? Is there a way to do it computationally without enumerating the possible combinations? 回答1: Always there is 2 n −1 non-empty subset of set {1,...,n} . For example consider the list ['a','b','c'] : >>>

Get number of instances of co-occurence of multiple (>2) characters

半世苍凉 提交于 2019-12-24 10:59:54
问题 This is an extension of the question here. I have a dataframe like so: df<-structure(list(person = c("p1", "p1", "p1", "p1", "p1", "p1", "p1", "p2", "p2", "p2", "p3", "p3", "p3", "p4", "p4", "p4", "p5", "p5", "p5", "p6", "p6", "p6", "p7", "p7", "p7"), hp_char = c("hp1", "hp2", "hp3", "hp4", "hp5", "hp6", "hp7", "hp8", "hp9", "hp10", "hp1", "hp2", "hp3", "hp5", "hp6", "hp7", "hp8", "hp9", "hp10", "hp3", "hp4", "hp5", "hp1", "hp2", "hp3")), class = c("tbl_df", "tbl", "data.frame"), row.names =

Creating a tree from a list of item combinations

时间秒杀一切 提交于 2019-12-24 10:59:27
问题 I have n lists A,B,C... which contain a,b,c... elements. I'm using them to create a lists of possible combinations (list of lists), such that first element is taken from table A, second from B etc. What is the best way to transform this flat structure to a tree which root is followed by elements of list A, each having all elements of list B etc. thus creating every possible combination in a form of a path from the root? 回答1: Algorithms 1: Going from the input So if you have the lists [1, 2, 3

What is an efficient way to find the minimum sum of multiple dictionary values, given keys of mutually exclusive integers?

依然范特西╮ 提交于 2019-12-24 08:10:46
问题 I have a dictionary, the keys of which consist of all len(4) combinations of the integers 0 to n , with all values being floats (representing a cost that was computed by another function). e.g.: cost_dict = {(0,1,2,3): 6.23, (0,1,2,4): 7.89, ... (14,15,16,17): 2.57} I would like to efficiently find m mutually exclusive keys (that is, where the keys do not share any of their integers) whose values sum to the lowest number (thus, finding the lowest overall cost). That is, I don't just want the

Iterating through combinations of groups of 4 within a group of 16

試著忘記壹切 提交于 2019-12-24 08:06:57
问题 Hi all , I know this question looks similar to some others but I have trawled through them extensively and can't get them to work for me. I have 16 datasets, let's call them 1 to 16. I would like to iterate through every possible different way of collecting these 16 into 4 groups; the most basic example being : [1,2,3,4][5,6,7,8][9,10,11,12][13,14,15,16]. The Question is how can I best iterate throught these combinations (in vba)? Below I have provided a more detailed example to help

How to get combination of two array ( vector ) by STL algorithm?

余生长醉 提交于 2019-12-24 07:41:13
问题 I have v1 and v2 , how should I got a new v like below? v1 = {1,2} v2 = {3,4,5} v = {f(1,3) , f(1,4) , f(1,5) f(2,3) ,f(2,4) ,f(2,5)} I know I could do it using two loops, But If there is more idiomatic way like using STL algorithm? //using two loops for iter1 of v1 for iter2 of v2 v.push_back(f(v1,v2)) EDIT: v1 and v2 not necessary have same size. 回答1: There is no appropriate STL algorithm, but this combination possible to do by a custom function and std::generate: #include <vector> #include

JavaScript - Generating combinations from dictionary keys and keeping key names dynamically

回眸只為那壹抹淺笑 提交于 2019-12-24 07:18:56
问题 I found this excellent code which generates all the combinations of multiple arrays here: JavaScript - Generating combinations from n arrays with m elements I am now looking to go a step further and I would like to generate all the combinations of a number of JSON objects containing arrays For example if I have the two objects within an array seen below: [{"Footprint_Shape":["L-Shape","H-Shape","T-Shape"]}, {"Num_of_Floors":[1,2]}] I would like to produce the array below which is every