combinations

JavaScript - Generating combinations from n arrays with m elements

情到浓时终转凉″ 提交于 2020-02-24 12:22:28
问题 I'm having trouble coming up with code to generate combinations from n number of arrays with m number of elements in them, in JavaScript. I've seen similar questions about this for other languages, but the answers incorporate syntactic or library magic that I'm unsure how to translate. Consider this data: [[0,1], [0,1,2,3], [0,1,2]] 3 arrays, with a different number of elements in them. What I want to do is get all combinations by combining an item from each array. For example: 0,0,0 // item

Number of all combinations in Knapsack task

試著忘記壹切 提交于 2020-02-14 12:15:19
问题 There is a classic Knapsack problem. My version of this problem is a little different. Given set of items, each with a mass, determine the number of combinations to pack items so that the total weight is less than or equal to a given limit. For example, there is 5 items with mass: 1, 1, 3, 4, 5 . There is a bug with limit = 7 . There are the following combinations: 1 + 3 1 + 4 1 + 5 1 + 1 + 3 1 + 1 + 4 1 + 1 + 5 3 3 + 4 4 5 Is there a way to count number of combinations without brute? 回答1:

All combinations of values from two lists representing a certain feature

自作多情 提交于 2020-02-14 02:27:07
问题 I have three lists: a = [0,1,2] b = [3,4,5] c = [aab, abb, aaa] How to create all three-element combinations? Where sequences from the list c tell you which list can be used to choose numbers for a given place in a given output sequence For example (pseudocode): for i=0 in range(len(c)): print: [0,1,3] [0,1,4] ... [0,2,5] ... [1,2,4] [1,2,5] And the same for the rest of the i indexes. Where the values in individual sublistas can not be repeated. I will be very grateful for any tips. 回答1: This

Combination of a Collection with Repetitions

好久不见. 提交于 2020-02-06 04:19:06
问题 There are a lot of links on http://stackoverflow.com for how to do combinations: Generating combinations in c++ But these links presume to draw from an infinite set without repetition. When given a finite collection which does have repetition, these algorithms construct duplicates. For example you can see the accepted solution to the linked question failing on a test case I constructed here: http://ideone.com/M7CyFc Given the input set: vector<int> row {40, 40, 40, 50, 50, 60, 100}; I expect

Combination of a Collection with Repetitions

江枫思渺然 提交于 2020-02-06 04:19:05
问题 There are a lot of links on http://stackoverflow.com for how to do combinations: Generating combinations in c++ But these links presume to draw from an infinite set without repetition. When given a finite collection which does have repetition, these algorithms construct duplicates. For example you can see the accepted solution to the linked question failing on a test case I constructed here: http://ideone.com/M7CyFc Given the input set: vector<int> row {40, 40, 40, 50, 50, 60, 100}; I expect

C++, not in order combination of array elements

≯℡__Kan透↙ 提交于 2020-02-05 03:31:07
问题 I am trying to get all combinations of an array with C++ such that double* list1 = new double[size]; Items in that array is {1,2,3,4,5} I need to add all possible combinations into a stack, such as: 1+2, 1+2+3, 1+2+3+4,1+2+3+4+5, 1+3, 1+3+4, 1+3+4+5, 1+4, 1+4+5, 1+5... the problem I am running into is I am doing this through 2 for loops and a while loop for(int i = 0; i < size - 1; i++) { for(int j = i; j < size - 1; j++) { double temp = list1[i] + list1[j + 1]; list1combos.push(temp); int k

Calculate all combinations of a series

天大地大妈咪最大 提交于 2020-02-03 09:58:17
问题 I have a list of items, and each item has a quantity. var items = { 1: 12, // we have 12 x item1 2: 1, // we have 1 x item2 3: 1, 4: 7, 5: 2, 6: 2 }; Alternatively this could be viewed as: var items = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6]; How would you go about obtaining a list of every combination of these items, keeping in mind that the order is entirely unimportant (and hence [1,2,3] == [3,2,1] ), and that not every item has to exist in the result. I

Calculate all combinations of a series

吃可爱长大的小学妹 提交于 2020-02-03 09:53:21
问题 I have a list of items, and each item has a quantity. var items = { 1: 12, // we have 12 x item1 2: 1, // we have 1 x item2 3: 1, 4: 7, 5: 2, 6: 2 }; Alternatively this could be viewed as: var items = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6]; How would you go about obtaining a list of every combination of these items, keeping in mind that the order is entirely unimportant (and hence [1,2,3] == [3,2,1] ), and that not every item has to exist in the result. I

Generate all combinations for a list of strings

那年仲夏 提交于 2020-01-27 06:15:34
问题 I want to generate a list of all possible combinations of a list of strings (it's actually a list of objects, but for simplicity we'll use strings). I need this list so that I can test every possible combination in a unit test. So for example if I have a list of: var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" } I need a List<List<string>> with all combinations like: A1 A2 A3 B1 B2 C1 A1 A2 A1 A2 A3 A1 A2 A3 B1 A1 A2 A3 B1 B2 A1 A2 A3 B1 B2 C1 A1 A3 A1 A3 B1 etc... A

Generate all combinations for a list of strings

╄→尐↘猪︶ㄣ 提交于 2020-01-27 06:14:47
问题 I want to generate a list of all possible combinations of a list of strings (it's actually a list of objects, but for simplicity we'll use strings). I need this list so that I can test every possible combination in a unit test. So for example if I have a list of: var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" } I need a List<List<string>> with all combinations like: A1 A2 A3 B1 B2 C1 A1 A2 A1 A2 A3 A1 A2 A3 B1 A1 A2 A3 B1 B2 A1 A2 A3 B1 B2 C1 A1 A3 A1 A3 B1 etc... A