combinations

C# split a list into all combinations of n groups - code migration from Python

ε祈祈猫儿з 提交于 2019-12-24 01:52:50
问题 There is a great implementation of the algorithm I am after here (by @lazy dog). However, I need this in c# and the conversion is not trivial due to C#'s lack of yield from and perhaps my own thickheadedness. Here is what I currently have: public static IEnumerable<ArrayList> sorted_k_partitions(int[] seq, int k) { var n = seq.Length; var groups = new ArrayList(); //a list of lists, currently empty IEnumerable<ArrayList> generate_partitions(int i) { if (i >= n) { // this line was the bug, was

C# split a list into all combinations of n groups - code migration from Python

跟風遠走 提交于 2019-12-24 01:52:07
问题 There is a great implementation of the algorithm I am after here (by @lazy dog). However, I need this in c# and the conversion is not trivial due to C#'s lack of yield from and perhaps my own thickheadedness. Here is what I currently have: public static IEnumerable<ArrayList> sorted_k_partitions(int[] seq, int k) { var n = seq.Length; var groups = new ArrayList(); //a list of lists, currently empty IEnumerable<ArrayList> generate_partitions(int i) { if (i >= n) { // this line was the bug, was

Print n-length combinations from char list using recursion

折月煮酒 提交于 2019-12-24 00:49:00
问题 I have to solve this exercise using recursion. Implement a function in Python which recieves as parameters list of characters and an integer n. The function has to print all the possible combinations in the length of n, where every character can be shown more than one time. It's very mind-blowing for me, all this thinking recursively generally. For example, for this problem, I think already one and a half hour, not knowing what I'm thinking about. I don't know how to start thinking

All possible combinations of card/poker hands for a set of players

北城以北 提交于 2019-12-23 22:39:13
问题 I am looking for an elegant(fast) python function that produces every combination from the following two arrays. cards = ["8H", "8S", "8C", "8D", "9H", "9S", "9C", "9D", "10H", "10S", "10C", "10D", "AH", "AS", "AC", "AD"] players = ["_1", "_1", "_1", "_2", "_2", "_2", "_3", "_3", "_3", "_4", "_4", "_4", "_To", "_To", "_To", "_Tc"] A combination would look like: [('8H', '_1'), ('8S', '_1'), ('8C', '_1'), ('8D', '_2'), ('9H', '_2'), ('9S', '_2'), ('9C', '_3'), ('9D', '_3'), ('10H', '_3'), ('10S

summing all possible combinations of an arbitrary number of arrays and applying limits and returning indices

北城余情 提交于 2019-12-23 21:09:33
问题 This is a modification of this question in which I would like to return the indices of the arrays elements in addition to the elements themselves. I've successfully modified arraysums() , arraysums_recursive() , but I'm struggling with arraysums_recursive_anyvals() . Here are the details: I modified arraysums() : def arraysums(arrays,lower,upper): products = itertools.product(*arrays) result = list() indices = itertools.product(*[np.arange(len(arr)) for arr in arrays]) index = list() for n,k

Combination of N elements with q elements in R

邮差的信 提交于 2019-12-23 20:11:01
问题 I have N=6 elements and q=3 elements symboled as 0 , 1 , 2 . I want to create all the vectors of N=6 elements with 2 elements equal to 0 , 2 elements equal to 1 and 2 elements equal to 2 in all the possible positions. The number of these vectors are equal to combn(6,2)*combn(4,2)*combn(2,2)=90 . Here is a code that construct these 90 vectors in the matrix F : N=6 x<-c(1:N) ######################################### A<-combn(x,2) B<-matrix(0,ncol(A),length(x)) for( i in 1:ncol(A) ) { y<-rep(0,N

Counting through all binary numbers with equal 1's and 0's

爱⌒轻易说出口 提交于 2019-12-23 18:52:56
问题 I'm implementing a binary representation of an equal-side bi-partitioning algorithm and I'm wondering what the best way to iterate through all combinations of N bits that have equal (N/2) 1's and 0's. I'm trying to find the quickest way, not the easiest to code. Thanks. 回答1: It's just (N choose N/2) ; you're choosing which bits are 0s, the rest are 1s. If you have 10 bits, and you want 5 zeroes and 5 ones, there are (10 choose 5) = 252 possibilities. See also: Algorithm to return all

Number of ways to go through a partially ordered set

时光毁灭记忆、已成空白 提交于 2019-12-23 18:00:44
问题 This is built upon a previous question Solve a simple packing combination with dependencies, although there is no need to check out that question to understand this one. This question asks about the fastest ways to count the number of linear extensions of a partially ordered set. Such an partially ordered list can be visualized as the feasibility of a packing configuration, given arbitrary ordering of the objects. The question is than equivalent to what are the possible ordering of all the

How can I use domain knowledge to improve this algorithm? Ugh, the weights

风流意气都作罢 提交于 2019-12-23 17:14:40
问题 I am having balancing trouble. I feel like I'm missing something here.. This problem equates to the following situation: On a table are a scattering of weights of various mass. In your hand are a few weights of various mass. If there is a combination of weights on the table that have a mass matching one in your hand, you can take those weights from the table, because they weigh the same as the single weight in your hand. You can drop the whole thing, including the weight from your hand, into

Using key combination in Java

别说谁变了你拦得住时间么 提交于 2019-12-23 16:28:50
问题 I develop a Java application and need to enable entering special symbols by using a 'dead' key or key modifiers. For example, the user should be able to enter 'ñ' by pressing '`+n'. I considered using key listeners but any event fired by a key is processed before the document is changed and I cannot alter that change. I also cannot change the key itself to allow direct entering the required symbol. Please advise. 回答1: I have found that KeyEvent.setKeyChar() method called from a key listener