permutation

Permutation of r elements out of n

谁说胖子不能爱 提交于 2019-12-25 01:06:21
问题 I have the following code for permutation. But it is throwing the below error. 11111 21111 31111 41111 51111 61111 71111 81111 91111 01111 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Permutation.main(nPr_3.java:22) The code is HashSet<Integer[]> set = new HashSet<Integer[]>(); int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int n = values.length; int r = 5; int i[] = new int[r]; int rc = 0; for(int j=0; j<Math.pow(n,r); j++) { Integer[] e = new Integer[r]; while

Generating permutations of a string in a range

冷暖自知 提交于 2019-12-24 23:31:42
问题 I am looking for an extremely efficient way to generate all possible permutations of a string (or alphabet), where the permutation length it bounded below and above by two variables (i, j) . So far I have been able to generate permutations a number of ways, e.g... void swap(char *x, char *y){ char w; w = *x; *x = *y; *y = w; } void permute(char *str, int start, int n){ int i; if(start == n-1) printf("%s\n", str); else for(i = start; i < n; i++){ swap(str+i, str+start); permute(str, start+1, n

Code to generate Permutations for a given set of numbers efficiently C#

╄→гoц情女王★ 提交于 2019-12-24 21:42:15
问题 Can anyone please write or give me a link where I can find the C# code to list all the permutations for a give set of numbers in the most efficient manner? 回答1: Not sure how efficient these are, but here are some options: http://www.codeproject.com/KB/recipes/Combinatorics.aspx http://www.codeproject.com/KB/recipes/premutations.aspx A C# implementation of Knuth's solution 回答2: A little bit too late... Just as reference... According to my tests, my implementation of Heap's algorithm seems to

Custom permutation, Equal distribution of equal size groups

老子叫甜甜 提交于 2019-12-24 18:53:03
问题 I made a post a few months ago here Custom permutation, Equal distribution of pairs. I wanted to generate pairs unique to each other while never containing the same pair. I got an excellent answer from Thierry Lathuille on here with this code. def pairs(n): for diff in range(1, n): starts_seen = set() index = 0 for i in range(n): pair = [index] starts_seen.add(index) index = (index+diff) % n pair.append(index) yield pair index = (index+diff) % n if index in starts_seen: index = (index+1) % n

Creating exhaustive case columns in R

浪子不回头ぞ 提交于 2019-12-24 18:36:56
问题 I am trying to generate a column "Gender Combinations" that creates exhaustive categories of interaction terms like in the table below. Is there an easy way to do this in R? +--------------+--------------+--------------+---------------------+ | EMP 1 Gender | EMP 2 Gender | Emp 3 Gender | Gender Combinations | +--------------+--------------+--------------+---------------------+ | Male | | | 1 Male | | Female | | | 1 Female | | | Male | | 1 Male | | | Female | | 1 Female | | | | Male | 1 Male

MemoryError while trying to using itertools.permutations, how use less memory?

*爱你&永不变心* 提交于 2019-12-24 16:28:33
问题 I'm loading from a text document containing so random strings and I'm trying to print every possible permutation of the characters in that string. If the notepad contains for example: 123 abc I want my output to be 123,132,213,231,312,321 abc,acb,bac,bca,cab,cba The text file contains some pretty large strings so I can see why I am getting this MemoryError. My first attempt I used this: import sys import itertools import math def organize(to_print): number_list = [] upper_list = [] lower_list

A good algorithm to generate a matrix containing all k-permutations of n elements with repetition?

♀尐吖头ヾ 提交于 2019-12-24 12:07:35
问题 So, let's say I have set S = {A1, A2} and I want to calculate all possible permutations of these two elements in groups of 3. I would like to generate a matrix such as this: (A1 A1 A2) (A1 A2 A1) (A2 A1 A1) (A2 A2 A1) (A2 A1 A2) (A1 A2 A2) I'm using R language. I've been trying to find some algorithm to generate a matrix like this, but haven't been successful so far. Thanks for your help. 回答1: One way to obtain all permutations would be to use expand.grid : a <- c("A", "B") expand.grid(a, a,

python get groups of combinations that each member appear only once

一个人想着一个人 提交于 2019-12-24 11:53:36
问题 I'm trying to get groups of permutations/combinations (r=2) that each member appear only once I used python 'combinations' package to have the combinations. For example: the members are: a,b,c,d. The combinations are: [a,b],[a,c],[a,d],[b,c],[b,d]... My desired output is: [ {[a,b],[c,d]},{[a,c],[b,d]},{[a,d],[b,c]}...] I would like to know what is the terminology for that case and if there is already implementation for that. Thanks. 回答1: Here's a way to do it: from itertools import

Code for generating all unique permutations recursively?

走远了吗. 提交于 2019-12-24 11:23:41
问题 Say I have a list L=[1,2,3,3,4] and I want to find all permutations of length 3 recursively. I am trying to return all unique permutations, which means something like [1,2,3] isn't included in the output twice because there are two 3 's in L . I ask because itertools.permutations includes duplicates, and also I am trying to iterate through the permutations in order (iterating from the lowest [1,2,3] up to [4,3,3] ) because I want to be able to quit iterating whenever I need to. I'm sorry if I

In R, generating every possible solution to a model, based on constraints

瘦欲@ 提交于 2019-12-24 11:23:03
问题 In R, I’m trying to generate a matrix that shows results from a model and the values used to solve them- all of which are constrained. Every possible solution. An example model: Model= a^2+b^2+c^2+d^2 Where: 20≤Model≤30 a=1 2 ≤b ≤3 2 ≤c ≤3 3 ≤d ≤4 I’d like the output to look like this: [a] [b] [c] [d] [Model] [1] 1 3 2 3 23 [2] 1 2 2 4 25 [3] 1 3 3 3 28 [4] 1 2 3 3 23 Order doesn't matter. I just want the full permutation of feasible [integer] values. Any packages or help you could point my