permutation

Find ith permutation in javascript

本秂侑毒 提交于 2019-12-17 18:37:26
问题 Given an array arr of size n , and and index 0<=i<n! I want to return the i'th permutation. I was able to write a method that gets all permutations: function permute (arr) { var permutations = []; if (arr.length === 1) { return [ arr ]; } for (var i = 0; i < arr.length; i++) { var subPerms = permute(arr.slice(0, i).concat(arr.slice(i + 1))); for (var j = 0; j < subPerms.length; j++) { subPerms[j].unshift(arr[i]); permutations.push(subPerms[j]); } } return permutations; } How do I trim it to

The amortized complexity of std::next_permutation?

五迷三道 提交于 2019-12-17 18:27:10
问题 I just read this other question about the complexity of next_permutation and while I'm satisfied with the response (O(n)), it seems like the algorithm might have a nice amortized analysis that shows a lower complexity. Does anyone know of such an analysis? 回答1: So looks like I'm going to be answering my own question in the affirmative - yes , next_permutation runs in O(1) amortized time. Before I go into a formal proof of this, here's a quick refresher on how the algorithm works. First, it

Johnson Trotter Algorithm

╄→гoц情女王★ 提交于 2019-12-17 16:52:54
问题 Overview: I am trying to implement the Johnson Trotter Algorithm in Java so that I can solve a problem on Project Euler. I have looked and looked but as far as I can see I have everything implemented right, which you know is wrong, otherwise I wouldn't be asking this question :) The basic algorithm goes like this: Johnson Trotter(n) //Input: A positive integer n //Output: A list of all permutations(0..n) initialize the first permutation with: <0, <1, <2 //(all elements pointing left) while (

Finding all possible case permutations in Python

大兔子大兔子 提交于 2019-12-17 16:49:12
问题 I need to work out a list of all possible permutations of case only in python for example with input of ar it would return [ 'ar','Ar','aR','AR'] or arc [ 'arc','Arc','ARc','aRc','aRC','ARC'] and I know there is likely some Nice method but for the life of me I can not figure it out. 回答1: def all_casings(input_string): if not input_string: yield "" else: first = input_string[:1] if first.lower() == first.upper(): for sub_casing in all_casings(input_string[1:]): yield first + sub_casing else:

All possible permutations of a set of lists in Python

岁酱吖の 提交于 2019-12-17 15:35:15
问题 In Python I have a list of n lists, each with a variable number of elements. How can I create a single list containing all the possible permutations: For example [ [ a, b, c], [d], [e, f] ] I want [ [a, d, e] , [a, d, f], [b, d, e], [b, d, f], [c, d, e], [c, d, f] ] Note I don't know n in advance. I thought itertools.product would be the right approach but it requires me to know the number of arguments in advance 回答1: You don't need to know n in advance to use itertools.product >>> import

vb .net permutation of string. permutation or combination?

好久不见. 提交于 2019-12-17 14:55:02
问题 i've got arary of string like this C - F - A - M . i want to create a combination from that with condition: each other item beside last character has to be combined with last character there's not allowed a same combination, even the order is different. for example FC - M CF - M if the string array contains >=3 element it will generate 2 & 3 itemset, if 2 element then it will generate only 2 itemset below is my code. my code generate the result like right part of the picture my question is

Python: How to append generator iteration values to a list

跟風遠走 提交于 2019-12-17 14:54:25
问题 I have a simple generator to give me permutations of a set of coordinates. I wish to save each new permutation to an element in an array using the code below: import random def poss_comb(coord): spin=random.shuffle if spin: spin(coord) yield (coord) ... a=[] for n in xrange(0,10): for item in poss_comb(coord): print item a.append(item) However when printing the results printing item gives me what I want : ['0 1', '', '1 2', '1 3'] ['0 1', '', '1 2', '1 3'] ['1 2', '0 1', '1 3', ''] ['0 1', '1

Algorithm to list unique permutations of string with duplicate letters

吃可爱长大的小学妹 提交于 2019-12-17 07:39:13
问题 For example, string "AAABBB" will have permutations: "ABAABB", "BBAABA", "ABABAB", etc What's a good algorithm for generating the permutations? (And what's its time complexity?) 回答1: For a multiset, you can solve recursively by position (JavaScript code): function f(multiset,counters,result){ if (counters.every(x => x === 0)){ console.log(result); return; } for (var i=0; i<counters.length; i++){ if (counters[i] > 0){ _counters = counters.slice(); _counters[i]--; f(multiset,_counters,result +

How to generate all permutations of an array in sorted order?

◇◆丶佛笑我妖孽 提交于 2019-12-17 06:40:17
问题 I have an array, and the user can insert a string. And I have this code: int main(){ char anagrama[13]; cin >> anagrama; for(int j = 0; j < strlen(anagrama); j++){ cout << anagrama[j]; for(int k = 0; k < strlen(anagrama); k++){ if(j != k) cout << anagrama[k]; } cout << endl; } } The problem is that I need all permutations of the string in sorted order. For example if the user write: abc , the output must to be: abc acb bac bca cab cba and my code doesn't show all permutations, and not sorted

Find the index of a given permutation in the sorted list of the permutations of a given string

喜夏-厌秋 提交于 2019-12-17 06:38:20
问题 We're given a string and a permutation of the string. For example, an input string sandeep and a permutation psdenae . Find the position of the given permutation in the sorted list of the permutations of the original string. 回答1: The total number of permutation of a given string of length n would be n! (if all characters are different), thus it would not be possible to explore all the combinations. This question is actually like the mathematics P & C question Find the rank of the word "stack"