combinations

LINQ implementation of Cartesian Product with pruning

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 03:03:12
问题 I hope someone is able to help me with what is, at least to me, quite a tricky algorithm. The Problem I have a List ( 1 <= size <= 5 , but size unknown until run-time) of Lists ( 1 <= size <= 2 ) that I need to combine. Here is an example of what I am looking at:- ListOfLists = { {1}, {2,3}, {2,3}, {4}, {2,3} } So, there are 2 stages to what I need to do:- (1). I need to combine the inner lists in such a way that any combination has exactly ONE item from each list, that is, the possible

Combinatorial iterator like expand.grid

元气小坏坏 提交于 2019-12-31 00:45:08
问题 Is there a fast way to iterate through combinations like those returned by expand.grid or CJ ( data.table ). These get too big to fit in memory when there are enough combinations. There is iproduct in itertools2 library (port of Python's itertools) but it is really slow (at least the way I'm using it - shown below). Are there other options? Here is an example, where the idea is to apply a function to each combination of rows from two data.frames (previous post). library(data.table) # CJ

generate all combintations for list with repeated items

随声附和 提交于 2019-12-30 11:21:14
问题 Related to this question, I am wondering the algorithms (and actual code in java/c/c++/python/etc., if you have!) to generate all combinations of r elements for a list with m elements in total. Some of these m elements may be repeated. Thanks! 回答1: Here is a recursion that I believe is closely related to Jean-Bernard Pellerin's algorithm, in Mathematica . This takes input as the number of each type of element. The output is in similar form. For example: {a,a,b,b,c,d,d,d,d} -> {2,2,1,4}

String Replacement Combinations

不羁的心 提交于 2019-12-30 10:54:28
问题 So I have a string '1xxx1' and I want to replace a certain number (maybe all maybe none) of x's with a character, let's say '5'. I want all possible combinations (...maybe permutations) of the string where x is either substituted or left as x. I would like those results stored in a list. So the desired result would be >>> myList = GenerateCombinations('1xxx1', '5') >>> print myList ['1xxx1','15xx1','155x1','15551','1x5x1','1x551','1xx51'] Obviously I'd like it to be able to handle strings of

Reversible “hash” function from 64-bit integer to 64-bit integer

痴心易碎 提交于 2019-12-30 08:37:06
问题 What I need is a reversible function that transforms a long (64-bit integer) into another long number, in a way that seems "random" for a user (but actually is deterministic), so that 3 subsequent numbers are transformed into 3 numbers completely different to each other. It is easy to do it without being reversible, but it turns out pretty difficult when it comes to this part. Basically it's the same question as Reversible hash function?, but I need more than 2^32 distinct values. Any ideas?

Efficiently computing vector combinations

会有一股神秘感。 提交于 2019-12-30 06:27:06
问题 I'm working on a research problem out of curiosity, and I don't know how to program the logic that I've in mind. Let me explain it to you: I've four vectors, say for example, v1 = 1 1 1 1 v2 = 2 2 2 2 v3 = 3 3 3 3 v4 = 4 4 4 4 Now what I want to do is to add them combination-wise, that is, v12 = v1+v2 v13 = v1+v3 v14 = v1+v4 v23 = v2+v3 v24 = v2+v4 v34 = v3+v4 Till this step it is just fine. The problem is now I want to add each of these vectors one vector from v1, v2, v3, v4 which it hasn't

Efficiently computing vector combinations

心已入冬 提交于 2019-12-30 06:26:19
问题 I'm working on a research problem out of curiosity, and I don't know how to program the logic that I've in mind. Let me explain it to you: I've four vectors, say for example, v1 = 1 1 1 1 v2 = 2 2 2 2 v3 = 3 3 3 3 v4 = 4 4 4 4 Now what I want to do is to add them combination-wise, that is, v12 = v1+v2 v13 = v1+v3 v14 = v1+v4 v23 = v2+v3 v24 = v2+v4 v34 = v3+v4 Till this step it is just fine. The problem is now I want to add each of these vectors one vector from v1, v2, v3, v4 which it hasn't

java string permutations and combinations lookup

雨燕双飞 提交于 2019-12-30 04:33:09
问题 I'm writing an Android word app. My code includes a method that would find all combinations of the string and the substrings of a 7 letter string with a minimum of length 3. Then compare all available combination to every word in the dictionary to find all the valid words. I'm using a recursive method. Here's the code. // Gets all the permutations of a string. void permuteString(String beginningString, String endingString) { if (endingString.length() <= 1){ if((Arrays.binarySearch(mDictionary

Python: Generating all ordered combinations of a list

感情迁移 提交于 2019-12-29 08:36:57
问题 I'm using Python 2.7. I'm having a list, and I want all possible ordered combinations. import itertools stuff = ["a","b","c", "d"] for L in range(1, len(stuff)+1): for subset in itertools.combinations(stuff, L): print( ' '.join(subset)) This will give the following output: a b c d a b a c <-- not in correct order a d <-- not in correct order b c b d <-- not in correct order c d a b c a b d <-- not in correct order a c d <-- not in correct order b c d a b c d But I want the output only to be

how to get all combination of an arraylist?

我的未来我决定 提交于 2019-12-29 08:25:09
问题 I have an arraylist of strings "abcde" I want to a method to return another arraylist with all the possible combination of a given arraylist (ex:ab,ac,ad...) in C# anyone knows a simple method? NB: all possible combinations of length 2, and would be better if the length is variable(can be changed) 回答1: Pertaining your comment requiring combinations of length two: string s = "abcde"; var combinations = from c in s from d in s.Remove(s.IndexOf(c), 1) select new string(new[] { c, d }); foreach