combinatorics

Array combinations without repetition

江枫思渺然 提交于 2019-11-29 08:21:50
I would like to make combinations out from an int[] {2,4,6,7,8,10,13,15,16,18} should give following results: 2,4,6 2,4,7 2,4,8 ... 15,16,18 Is it possible to write query only solution without using custom functions? with a as ( select i from unnest (array[2,4,6,7,8,10,13,15,16,18]) s(i) ) select * from a cross join a b cross join a c where a < b and b < c order by a, b, c 来源: https://stackoverflow.com/questions/16899760/array-combinations-without-repetition

combination without repetition of N elements without use for..to..do

时光总嘲笑我的痴心妄想 提交于 2019-11-29 08:19:05
问题 i want load in a list the combination of N number without repetition, giving to input the elements and group. For example, with 4 elements [1,2,3,4], i have for: Group 1: [1][2][3][4]; Group 2: [1,2][1,3][1,4][2,3][2,4][3,4]; Group 3: [1,2,3][1,2,4][1,3,4][2,3,4] Group 4: [1,2,3,4] Now, i have solved it using nested loop for, for example with group 2, i write: for x1 := 1 to 3 do for x2 := Succ(x1) to 4 do begin // x1, x2 // end or for group 3, i wrote: for x1 := 1 to 2 do for x2 := Succ(x1)

Computing the Factoradic Rank of a Permutation (N choose K)

岁酱吖の 提交于 2019-11-29 08:12:26
I've recently learnt about CNS and FNS , and since they look so elegant to me, I decided to try and implement methods to generate combinations and permutations using those techniques. I finished my method to convert from n choose k combinations to a CSN rank and vice-versa but I'm banging my head against the wall trying to do the same with n choose k (unique) permutations. Thanks to @Joshua I got the unranking (FNS to permutation) method working: function Pr_Unrank($n, $k, $rank) { // rank starts at 1 if ($n >= $k) { if (($rank > 0) && ($rank <= Pr($n, $k))) { $rank--; $result = array();

C# LINQ combinatorics: All Combinations of a Set without the Empty Set

最后都变了- 提交于 2019-11-29 07:06:26
I have a set of strings, and I want to find all possible combinations of the strings and add them to a list. I want to end up with a list of a list of each combination of the strings, minus the empty set. I have created a solution that does this exactly with a nested for loop. However I want to do this more elegantly, preferably with LINQ , and I am not so proficient with it because I'm still pretty new to it. The solution should have 2^n - 1 lists of combinations where n is the cardinality of the original set. Here is a correct example of what I am looking for: set = {a, b, c}

In Perl, how can I generate all possible combinations of a list?

核能气质少年 提交于 2019-11-29 05:40:37
I have a file with a list, and a need to make a file that compares each line to the other. for example, my file has this: AAA BBB CCC DDD EEE I would like the final list to look like this: AAA BBB AAA CCC AAA DDD AAA EEE BBB CCC BBB DDD BBB EEE CCC DDD CCC EEE DDD EEE I am trying to do this in Perl, for this first time and am having a little trouble. I do know that you need to make an array, and then split it, but after that I am having some trouble. Use Algorithm::Combinatorics . The iterator based approach is preferable to generating everything at once. #!/usr/bin/env perl use strict; use

Picking unordered combinations from pools with overlap

拈花ヽ惹草 提交于 2019-11-29 05:17:34
问题 I have pools of values and I would like to generate every possible unordered combination by picking from certain pools. For example, I wanted to pick from pool 0, pool 0, and pool 1: >>> pools = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] >>> part = (0, 0, 1) >>> list(product(*(pools[i] for i in part))) [(1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 2), (2, 3, 3), (2, 3, 4), (3,

R: Generating all permutations of N weights in multiples of P

耗尽温柔 提交于 2019-11-29 05:14:24
I need to create a function (in R) which: - given N possible variables to attribute weights to; - creates all possible permuations of weights (summing to 100%); - subject to the constraint that weights must occur in multiples of P (usually 1%) Obviously, as N and P are inversely related - i.e. I can't specify N =7, and P =0.4. However, I'd like to be able to specify integer solutions only, i.e. P =0.01. Sorry if this is a well-known problem - I'm not a math person, and I've searched using terms I know, but didn't find anything close enough. I'd post the code I've written, but.. it's not

splitting list in chunks of balanced weight

感情迁移 提交于 2019-11-29 04:21:06
I need an algorithm to split a list of values into such chunks, that sum of values in every chunk is ( approximately ) equals (its some variation of Knapsack problem , I suppose) So, for example [1, 2, 1, 4, 10, 3, 8] => [[8, 2], [10], [1, 3, 1, 4]] Chunks of equal lengths are preferred, but it's not a constraint. Python is preferred language, but others are welcome as well Edit: number of chunks is defined Greedy: 1. Order the available items descending. 2. Create N empty groups 3. Start adding the items one at a time into the group that has the smallest sum in it. I think in most real life

Calculate n-ary Cartesian Product

三世轮回 提交于 2019-11-29 03:55:10
Given two lists, I can produce a list of all permutations the Cartesian Product of these two lists: permute :: [a] -> [a] -> [[a]] permute xs ys = [ [x, y] | x <- xs, y <- ys ] Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ] How do I extend permute so that instead of taking two lists, it takes a list (length n) of lists and returns a list of lists (length n) permute :: [[a]] -> [[a]] Example> permute [ [1,2], [3,4], [5,6] ] == [ [1,3,5], [1,3,6], [1,4,5], [1,4,6] ] --etc I couldn't find anything relevant on Hoogle.. the only function matching the signature was transpose , which

The Assignment Problem, a NumPy function?

微笑、不失礼 提交于 2019-11-29 03:47:10
Since an assignment problem can be posed in the form of a single matrix, I am wondering if NumPy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if NumPy/SciPy has an assignment-problem-solve function? Edit: In the meanwhile I have found a Python (not NumPy/SciPy) implementation at http://software.clapper.org/munkres/ . Still I suppose a NumPy/SciPy implementation could be much faster, right? No, NumPy contains no such function. Combinatorial optimization is outside of NumPy's scope. It may be possible to do it with one of the optimizers in scipy