combinations

combinations with varying length and without repetition

吃可爱长大的小学妹 提交于 2020-01-01 19:46:12
问题 Can anyone suggest an easy way of getting all combinations without repeats, and with a varying length ? [0,1,2,3,4] (2's) : [0,1],[0,2],[0,3] ... [3,4] (3's) : [0,1,2],[0,1,3] ... [2,3,4] (4's) : [0,1,2,3],[0,1,2,4] ... [1,2,3,4] 回答1: took me a while but I think I've got it here... this has all the combinations without repeats var arr:Array = [0, 1, 2, 3, 4]; var $allcombos:Array = []; findCombos($allcombos,[],arr); function findCombos($root:Array, $base:Array, $rem:Array):void { for (var i

Minimize the sum of errors of representative integers

隐身守侯 提交于 2020-01-01 02:07:43
问题 Given n integers between [0,10000] as D 1 ,D 2 ...,D n , where there may be duplicates, and n can be huge: I want to find k distinct representative integers (for example k=5) between [0,10000] as R 1 ,R 2 ,...,R k , so the sum of errors of all the representative integers is minimized. The error of a representative integer is defined below: Assuming we have k representative integers in ascending order as {R 1 ,R 2 ...,R k }, the error of R i is : and i want to minimize the sum of errors of the

Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists

萝らか妹 提交于 2020-01-01 00:48:09
问题 I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of all possible 2-list combinations in this dataset. Note that elements in each list are unique so they can be stored as sets in python. What is the most efficient way to perform this in python? Edit I forgot to specify that I need to have the ability to match the intersection values to the corresponding pair of lists. Thanks everybody for the prompt response and

All combinations of elements of two lists in Haskell

回眸只為那壹抹淺笑 提交于 2019-12-31 19:28:51
问题 Given two lists, [a, b] and [c, d] , I'd like to get the following result: [(a,c), (a,d), (b,c), (b,d)] How can I do this in Haskell? Is there a built-in function for this, or should I implement one myself? 回答1: [ (x,y) | x<-[a,b], y<-[c,d] ] This doesn't really require any further explanation, does it? 回答2: Applicative style all the way! λ> :m + Control.Applicative λ> (,) <$> ['a','b'] <*> ['c','d'] [('a','c'),('a','d'),('b','c'),('b','d')] (I've eschewed any String syntactic sugar above, in

Sum of all number combinations excluding repetition Excel

我们两清 提交于 2019-12-31 07:44:31
问题 this top voted answer Sum all combinations Excel or Google Spreadsheets is close to what I need - however I require a solution where it keeps the combinations in order and excludes repetition based on this order. i.e. a data set of 1 2 3 4 would show the product of: 1 1+2 1+3 1+4 1+2+3 1+2+4 1+3+4 1+2+3+4 2 2+3 2+4 2+3+4 3 3+4 4 i.e 1 3 4 5 6 10 2 5 6 3 7 4 Is this possible in Google Sheets / Excel without a script? Thanks 回答1: Here's a formula to display the sums in order =ArrayFormula(sort(

Given a string of integers find out all the possible words that can made out of it in continuous order. [closed]

谁说胖子不能爱 提交于 2019-12-31 07:42:18
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Given a string of integers how to find out all the possible words that can made out of it in continuous order. Eg: 11112 ans: AAAAB AKAB AAKB AAAL etc. public static void main(String[] args) { String str="11111124"; char strChar[]=str.toCharArray(); String target=""; for(int i=0;i<strChar.length;i++) { target

how to produce every permutation of positioning 20 values of -1 in a 1-by-41 vector of ones?

牧云@^-^@ 提交于 2019-12-31 04:50:11
问题 I have written different code to produce different permutations of ones and minus ones. they work for matrixes with small dimensions: for example: S=[-1 -1 1 1 1 1 1 1]; P=unique(perms(S),'rows'); produces: -1 -1 1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 -1 1 1 -1 1 1 1 1 -1 1 1 1 -1 1 1 1 -1 1 1 1 1 -1 1 1 -1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 1 -1 1 -1 -1 1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 -1 1 1 -1 1 1 1 1 -1 1 1 1 -1 1 1 1 -1 1 1 1 1 -1 1 1 -1 1 1 1 1 1 -1 1 1 -1 -1 1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 -1 1 1 -1 1 1 1

Sql query to return one single record per each combination in a table

怎甘沉沦 提交于 2019-12-31 04:39:04
问题 I need the result for every combination of (from_id, to_id) which has the minimun value and the loop matching a criteria. So basically I need the loop that has the minimun value. e.g. From A to B i need the minimun value and the loop_id . The table has the following fields: value from_id to_id loop_id ------------------------------------- 2.3 A B 2 0.1 A C 2 2.1 A B 4 5.4 A C 4 So a result will be: value from_id to_id loop_id ------------------------------------- 2.1 A B 4 0.1 A C 2 I have

Python combination with replacement

点点圈 提交于 2019-12-31 03:56:25
问题 Below is my code import itertools a = [1,2,3] for i in itertools.combination_with_replacement(a,3): print i Output (1, 1, 1),(1, 1, 2) (1, 1, 3),(1, 2, 2) (1, 2, 3),(1, 3, 3) (2, 2, 2),(2, 2, 3) (2, 3, 3),(3, 3, 3) Only 10 result is print out, but by formula, it should be 3^3 = 27 output. So may i know, how to get the other output? Sincerely thank for your time and suggestion. 回答1: You want cartesian product, not combinations. import itertools print list(itertools.product([1, 2, 3], repeat=3)

What is a good way to implement choose notation in Java?

元气小坏坏 提交于 2019-12-31 03:36:08
问题 ... preferably in Java. Here is what I have: //x choose y public static double choose(int x, int y) { if (y < 0 || y > x) return 0; if (y == 0 || y == x) return 1; double answer = 1; for (int i = x-y+1; i <= x; i++) { answer = answer * i; } for (int j = y; j > 1; j--) { answer = answer / j; } return answer; } I'm wondering if there's a better way of doing this? 回答1: choose(n,k) = n! / (n-k)! k! You could do something like this in O(k): public static double choose(int x, int y) { if (y < 0 ||