combinatorics

Array combinations without repetition

不打扰是莪最后的温柔 提交于 2019-11-28 01:47:08
问题 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? 回答1: 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

Get all possible word combinations

此生再无相见时 提交于 2019-11-28 01:37:42
I have a list of n words (let's say 26). Now I want to get a list of all possible combinations, but with a maximum of k words per row (let's say 5) So when my word list is: aaa, bbb, ..., zzz I want to get: aaa bbb ... aaabbb aaaccc ... aaabbbcccdddeeefff aaabbbcccdddeeeggg ... I want to make it variable, so that it will work with any n or k value. There should be no word be twice and every combinations needs to be taken (even if there are very much). How could I achieve that? EDIT: Thank you for your answers. It is not an assignment. Is is just that I forgot the combinations of my password

Finding all possible value combinations between two arrays

蓝咒 提交于 2019-11-28 01:21:08
I have two arrays of strings, not necessarily of the same length, I want to find all the possible "sets" of combinations between two values from the arrays, without repeats from either array. For example, given the arrays: { "A1", "A2", "A3" } { "B1", "B2" } The result I want is the following sets: { ("A1", "B1"), ("A2", "B2") } { ("A1", "B1"), ("A3", "B2") } { ("A1", "B2"), ("A2", "B1") } { ("A1", "B2"), ("A3", "B1") } { ("A2", "B1"), ("A3", "B2") } { ("A2", "B2"), ("A3", "B1") } My general direction is to create recursive function that takes as a parameter the two arrays and removes each

Combinatorics: generate all “states” - array combinations

无人久伴 提交于 2019-11-28 00:07:08
I have an array of integers: n[] . Also, I have an array ( Nr[] ) contains n.length integers. I need to generate all combinations of n[] in a following way: /* let n.length == 3 and Nr[0] = 2, Nr[1] = 3, Nr[2] = 3 */ n = {0, 0, 0}; n = {1, 0, 0}; n = {2, 0, 0}; n = {0, 1, 0}; n = {0, 2, 0}; n = {0, 3, 0}; n = {0, 0, 1}; ... n = {1, 1, 0}; n = {1, 2, 0}; n = {1, 3, 0}; n = {2, 1, 0}; n = {2, 2, 0}; n = {2, 3, 0}; n = {1, 1, 1}; ... n = {0, 1, 1}; // many others The goal is to find all combinations of n , where n[i] can be 0 to Nr[i] . I did not succeed... How to solve it in Java? Or not in Java

permutations/combinatorics library for java? [closed]

混江龙づ霸主 提交于 2019-11-27 23:50:19
问题 I am looking for a library for java that will generate all possible order permutations of a set. The only library I can find is combinatoricslib on google code. I find it very hard to believe this is the only java library that does this, and am quite frankly very surprised by this. Is there anything in the JDK, or apache commons math, or another library, that provides this same functionality? I am happy to use combinatoricslib, I just can't believe that's the only option, other than writing

Next Composition of n into k parts - does anyone have a working algorithm?

梦想的初衷 提交于 2019-11-27 22:26:02
Composition of n into k parts - I want to list all the possible compositions of n into k parts - does anyone have an algorithm (preferably in R)? Or know if it's in library anywhere? For example, if I have n cubes, and k bags, and want to list all the possible arrangements of the cubes in the bags. e.g. there are 3 ways you can arrange 2 cubes into 2 bags: (2, 0) (1, 1) (0, 2) I've found the NEXCOM alogarithm. I've found a version of it here (page 46) in Fortran, but don't code in Fortran so really understand what's going on - any help? What you are attempting to list is called an k

splitting list in chunks of balanced weight

江枫思渺然 提交于 2019-11-27 18:14:35
问题 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 回答1: Greedy: 1. Order the available items descending. 2. Create N empty groups 3. Start

android lock password combinations

天大地大妈咪最大 提交于 2019-11-27 18:12:28
I just came across with this interesting question from my colleague. I'm trying now, but meanwhile I thought I could share it here. With the password grid shown in the Android home screen, how many valid passwords are possible? min password length: 4 max: 9 (correct me if I'm wrong) Dante May Code Summary The full combinations of 4 to 9 distinctive numbers, minus the combinations which include invalid "jump"s. The Long Version The rule for Android 3x3 password grid: one point for once cannot "jump" over a point The author of the original post used Mathematica to generate all 985824

Calculate n-ary Cartesian Product

妖精的绣舞 提交于 2019-11-27 17:51:54
问题 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

Scheduling algorithm for a round-robin tournament?

不想你离开。 提交于 2019-11-27 17:45:30
I recently did studying stuff and meet up with Donald Knuth. But i didn't found the right algorithm to my problem. The Problem We have a league with n players. every week they have a match with one other. in n-1 weeks every team fought against each other. there are n/2 matches a day. but one team can only fight once in a week. if we generate an (n/k) combination we get all of the combinations... (assuming k = 2) but i need to bring them in the right order. My first suggestion was... not the best one. i just made an array, and then let the computer try if he finds the right way. if not, go back