combinations

Stuck with Combination Problem

若如初见. 提交于 2019-12-21 06:43:14
问题 I'm having a problem w/ my program. I have extracted a set of data and I would like to test if there is a combination for a particular number. For example, I have an array of int, 1 2 3 4 5, I would like to know if there is a combination for 7 maybe, and it must answer yes there is 3 + 4. I figured out that I need to use the combination formula. So I thought that the outer loop may go like 5C1..5C2..5C3..etc, starting to "take 1" then "take 2" at a time to find out all the possible

What is the big-O complexity of this naive code to compute combinations?

喜夏-厌秋 提交于 2019-12-21 05:12:07
问题 The following recursive algorithm is a (fairly inefficient) way to compute n choose k: int combinationsOf(int n, int k) { if (k == 0) return 1; if (n == 0) return 0; return combinationsOf(n - 1, k) + combinationsOf(n - 1, k - 1); } It is based on the following recursive insight: Actually evaluating this function takes a LOT of function calls. For example, computing 2 choose 2 this way makes these calls: combinationsOf(2, 2) | | | +- combinationsOf(1, 2) | | | | | +- combinationsOf(0, 2) | | |

Getting all combinations of pairs from a list in Ruby

廉价感情. 提交于 2019-12-21 04:51:36
问题 I have a list of elements (e.g. numbers) and I want to retrieve a list of all possible pairs. How can I do that using Ruby? Example: l1 = [1, 2, 3, 4, 5] Result: l2 #=> [[1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5]] 回答1: In Ruby 1.8.6, you can use Facets: require 'facets/array/combination' i1 = [1,2,3,4,5] i2 = [] i1.combination(2).to_a # => [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]] In 1.8.7 and later, combination is built-in: i1

Algorithm for finding smallest collection of components

让人想犯罪 __ 提交于 2019-12-21 04:21:33
问题 I'm looking for an algorithm to solve the following problem. I have a number of subsets (1-n) of a given set (a-h). I want to find the smallest collection of subsets that will allow me to construct, by combination, all of the given subsets. This collection can contain subsets that do not exist in 1-n yet. a b c d e f g h 1 1 2 1 1 3 1 1 1 4 1 1 5 1 1 6 1 1 1 1 7 1 1 1 1 8 1 1 1 9 1 1 1 Below are two possible collections, the smallest of which contains seven subsets. I have denoted new subsets

Generating a numpy array with all combinations of numbers that sum to less than a given number

北战南征 提交于 2019-12-21 03:59:25
问题 There are several elegant examples of using numpy in Python to generate arrays of all combinations. For example the answer here: Using numpy to build an array of all combinations of two arrays . Now suppose there is an extra constraint, namely, the sum of all numbers cannot add up to more than a given constant K . Using a generator and itertools.product , for an example with K=3 where we want the combinations of three variables with ranges 0-1,0-3, and 0-2 we can do it a follows: from

Permutations with MapReduce

一笑奈何 提交于 2019-12-21 02:41:38
问题 Is there a way to generate permutations with MapReduce? input file: 1 title1 2 title2 3 title3 my goal: 1,2 title1,title2 1,3 title1,title3 2,3 title2,title3 回答1: Since a file will have n inputs, the permutations should have n^2 outputs. It makes sense that you could have n tasks perform n of those operations. I believe you could do this (assuming only for one file): Put your input file into the DistributedCache to be accessible as read-only to your Mapper/Reducers. Make an input split on

Place “sum” and “multiply” operators between the elements of a given list of integers so that the expression results in a specified value

杀马特。学长 韩版系。学妹 提交于 2019-12-20 12:11:09
问题 I was given a tricky question. Given: A = [a1,a2,...an] (list of positive integers with length "n") r (positive integer) Find a list of { *, + } operators O = [o1,o2,...on-1] so that if we placed those operators between the elements of "A", the resulting expression would evaluate to "r". Only one solution is required. So for example if A = [1,2,3,4] r = 14 then O = [*, +, *] I've implemented a simple recursive solution with some optimisation, but of course it's exponential O(2^n) time, so for

Smart way to generate permutation and combination of String

风格不统一 提交于 2019-12-20 09:49:05
问题 String database[] = {'a', 'b', 'c'}; I would like to generate the following strings sequence, based on given database . a b c aa ab ac ba bb bc ca cb cc aaa ... I can only think of a pretty "dummy" solution. public class JavaApplication21 { /** * @param args the command line arguments */ public static void main(String[] args) { char[] database = {'a', 'b', 'c'}; String query = "a"; StringBuilder query_sb = new StringBuilder(query); for (int a = 0; a < database.length; a++) { query_sb

Listing permutations of different combination of characters

别说谁变了你拦得住时间么 提交于 2019-12-20 07:50:35
问题 The closest SO topic I found is here: Listing all permutations of a string/integer But how would I make use of this for different sets of characters for each position in a string? An example: I specify a string length of "3". The first two position should be either "a" or "b", but the last position should be either "1" or "2", e.g: aa1 ba1 ab1 bb1 aa2 ab2 ba2 bb2 回答1: Use this code: public static List<string> GenerateCombinations(char[][] characters) { var combinations = new List<string>();

Generating all the combinations of two lists and output them one by one in python

∥☆過路亽.° 提交于 2019-12-20 07:39:45
问题 I have two lists [1, 3, 4] [7, 8] I want to generate all the combinations of two list starting from the smallest combinations like 17,18,37,38,47,48,137,138,147,148......178,378.... Now for each combination I have to test it's presence in some other place and if I found that combination to be present then I will stop the combination generation. For example If I see that 17 is present then I will not generate the other combinations. Again If I found 48 to be present then I will not generate