permutation

permute a string

淺唱寂寞╮ 提交于 2019-12-22 11:24:57
问题 Given the String, print all its permutations. To do that, i came up with the following program. public static char[] swap(char[] input, int i, int j) { char temp; temp = input[i]; input[i] = input[j]; input[j] = temp; return input; } /** * * @param args */ public static void permuteStrings(char[] inputString, int start, int finish ) { //Base case: When there is only single element, print the string if(start == finish) System.out.println(inputString); else { //Recursive case: Swap first

How to parallelize an array shift with OpenMP?

淺唱寂寞╮ 提交于 2019-12-22 10:35:06
问题 How can I parallelize an array shift with OpenMP? I've tryed a few things but didn't get any accurate results for the following example (which rotates the elements of an array of Carteira objects, for a permutation algorithm): void rotaciona(int i) { Carteira aux = this->carteira[i]; for(int c = i; c < this->size - 1; c++) { this->carteira[c] = this->carteira[c+1]; } this->carteira[this->size-1] = aux; } Thank you very much! 回答1: This is an example of a loop with loop-carried dependencies,

Permutations with repetition using recursion - JavaScript

感情迁移 提交于 2019-12-22 09:57:43
问题 I'm working on a recursive algorithm that takes in an array with three different elements (say ['a', 'b', 'c'] and returns a two-dimensional array with all the possible variations with repetition allowed (so [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'b', 'b'],...] ). However my code fails and I'm not sure why. var abc = function () { var holdingArr = []; var threeOptions = ["a", "b", "c"]; var singleSolution = []; var recursiveABC = function(arr) { if (singleSolution.length > 2) { holdingArr

Return non-duplicate random values from a very large range

二次信任 提交于 2019-12-22 09:37:40
问题 I would like a function that will produce k pseudo-random values from a set of n integers, zero to n-1, without repeating any previous result. k is less than or equal to n. O(n) memory is unacceptable because of the large size of n and the frequency with which I'll need to re-shuffle. These are the methods I've considered so far: Array : Normally if I wanted duplicate-free random values I'd shuffle an array, but that's O(n) memory. n is likely to be too large for that to work. long nextvalue

(Nearly) Evenly select items from a list

寵の児 提交于 2019-12-22 08:53:36
问题 I have a list of N elements, and I'd like to sample M (<= N) values which are as evenly spaced as possible. To be more specific, lets say the selection should minimize the differences in the spacings between sampled points. For example, lets say I'm constructing a boolean indexing array (i.e. in python ) to select elements, I tried the algorithm (from this similar, but different question: How do you split a list into evenly sized chunks?) : q, r = divmod(N, M) indices = [q*jj + min(jj, r) for

Quick way to find all permutations of a pandas DataFrame that preserves a sort?

别说谁变了你拦得住时间么 提交于 2019-12-22 08:38:29
问题 I have a DataFrame, and I'd like find all the permutations of it that fulfill a simple ascending sort on one of the columns. (There are many ties.) For example, in the following DataFrame df = pd.DataFrame({'name': ["Abe", "Bob", "Chris", "David", "Evan"], 'age': [28, 20, 21, 22, 21]}) I'd be looking to sort by age and obtain the orders ["Bob", "Chris", "Evan", "David", "Abe"] and ["Bob", "Evan", "Chris", "David", "Abe"] . I'm new to python (and to pandas) and curious if there is a simple way

How to write a good hashCode() for permutations?

烂漫一生 提交于 2019-12-22 06:48:55
问题 In my program, I handle a lot of lists of size n that all are permutations of [1,..., n ]. My problem is that I put these permutations in HashMap s and HashSet s, and I need a good hashCode() that avoids too many collisions. All the solutions I have thought of lead to either a lot of collisions or an overflow. How can I write a good hashCode for permutations ? 回答1: Have you tried a 'rotating hash'? You could adjust the barrel rotate amount to see if it makes much difference to the hash

Generating a list of repetitions regardless of the order

橙三吉。 提交于 2019-12-22 05:52:45
问题 I want to generate combinations that associate indices in a list with "slots". For instance, (0, 0, 1) means that 0 and 1 belong to the same slot while 2 belongs to an other. (0, 1, 1, 1) means that 1, 2, 3 belong to the same slot while 0 is by itself. In this example, 0 and 1 are just ways of identifying these slots but do not carry information for my usage. Consequently, (0, 0, 0) is absolutely identical to (1, 1, 1) for my purposes, and (0, 0, 1) is equivalent to (1, 1, 0) . The classical

Finding permutation of a set of 0 and 1, given index with O(N)

一世执手 提交于 2019-12-22 05:10:32
问题 I am trying to find the most efficient way to find permutation on a set of '0' and '1' given an index. Ex: Given l = [0, 0, 1, 1]. All permutations in an ascending order is {0011, 0101, 0110, 1001, 1010, 1100}. These elements are indexed from 0 -> 5. Given index = 2, the result is 0110. I found the algorithm here which inputs an integer multiset (e.g. l = [1, 2, 2]). His algorithm is efficient (O(N^2)). However, my multiset only consists of '0' and '1' and requires O(N) or less. N is the

Go through all permutations of an array recursively

拥有回忆 提交于 2019-12-21 16:59:44
问题 I am trying to write a recursive function to produce all permutations of an array. static int permus[] = new int[] { 1, 2, 3, 4, 5 }; static void testPermu(int start) { // Print it System.out.println(Arrays.toString(permus)); int k; for (int i = start + 1; i < permus.length; i++) { // swap k = permus[start]; permus[start] = permus[i]; permus[i] = k; testPermu(i); // unswap k = permus[start]; permus[start] = permus[i]; permus[i] = k; } } It's invoked as testPermu(0) and should produce all