permutation

Permutation algorithm for n characters in x positions

时光毁灭记忆、已成空白 提交于 2021-01-28 10:26:40
问题 e.g. A permutation algorithm for {&, *, %} to be placed in 8 positions: &&&&&&&&& &&&&&&&&* &&&&&&&&% &&&&&&&*% &&&&&&&** ... Heap's permutation algorithm I saw on the Internet works just for those who the number of characters are equal to the number of positions, And those who can work with unequal number of characters and number of positions, Only work with integers, Not characters. I also have to say I haven't reached any working algorithm up to now, As I don't know anything about

Find all the possible N-length anagrams - fast alternatives

為{幸葍}努か 提交于 2021-01-28 07:43:10
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

Find all the possible N-length anagrams - fast alternatives

♀尐吖头ヾ 提交于 2021-01-28 07:21:18
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

Python Itertools permutations with strings

岁酱吖の 提交于 2021-01-28 07:10:15
问题 i want to use itertools permutations for strings instead of just letters. import itertools lst = list(permutations(("red","blue"),3)) #This returns [] I know i can do something like: a = list(permutations(range(3),3)) for i in range(len(a)): a[i] = list(map(lambda x: 'red' if x==0 else 'blue' if x==1 else 'green',a[i])) EDIT: I want to key in this as my input, and get this as my output input: ("red","red","blue") output: [(’red’, ’red’, ’red’), (’red’, ’red’, ’blue’),\ (’red’, ’blue’, ’red’),

Javascript Permutations magic trick

a 夏天 提交于 2021-01-28 06:33:12
问题 I'm reading some algorithms to try and understand permutations in javascript, and the following one quite stunned me var permArr = [], usedChars = []; function permute(input) { var i, ch, chars = input.split(''); for (i = 0; i < chars.length; i++) { ch = chars.splice(i, 1); usedChars.push(ch); if (chars.length == 0) permArr[permArr.length] = usedChars.join(''); permute(chars.join("")); chars.splice(i, 0, ch); usedChars.pop(); } return permArr } FYI I found this algorithm on the following

Generating all possible sets of 3 groups of 6 numbers chosen from the integers 1-18

别说谁变了你拦得住时间么 提交于 2021-01-28 06:07:19
问题 Since the groups of 6 represent dice, the order of the integers in these groups does not matter. The order of the groups within each set does matter, but to check each one it would be simple to find the permutations of each set, which I can do. Now, I've come up with a way to achieve all this, the problem is efficiency. I want to check each set but so far I've only come up with a way using: for i in itertools.combinations(num, 6): W.append([i]) print(W) print("Done building list of ",len(W),"

How to find the index of the permutation

匆匆过客 提交于 2021-01-28 02:51:01
问题 % index(+List, -Idx) Predicate will get List with permutation and I want to know index of permutation For example: ?- index([4,1,3,2],X). X= 19. My solution: index([],0). index([_],1). index([X,Y],2):- Y > X. index([H,X|T],Idx):-index([X|T],Idx+1),H > X. Why is it wrong? And how can I make incremention of Idx? 回答1: I found cleaner version of same idea so I show the code: permutation_index([X|Xs], I) :- prerequisite( ( sort([X|Xs], S), length([X|Xs], Len), length(S, Len) )), permutation_index

How to find the index of the permutation

只谈情不闲聊 提交于 2021-01-28 00:00:52
问题 % index(+List, -Idx) Predicate will get List with permutation and I want to know index of permutation For example: ?- index([4,1,3,2],X). X= 19. My solution: index([],0). index([_],1). index([X,Y],2):- Y > X. index([H,X|T],Idx):-index([X|T],Idx+1),H > X. Why is it wrong? And how can I make incremention of Idx? 回答1: I found cleaner version of same idea so I show the code: permutation_index([X|Xs], I) :- prerequisite( ( sort([X|Xs], S), length([X|Xs], Len), length(S, Len) )), permutation_index

Steinhaus–Johnson–Trotter algorithm with an arbitrary initial state

蓝咒 提交于 2021-01-27 20:21:01
问题 What must be changed in a standard Steinhaus–Johnson–Trotter if values in the initial array are not ordered? For example, my initial array is 312 and I want to generate the following result: 312 321 231 213 123 132 I could introduce an additional array which defines the initial weight of each number, e.g. w[3]=1, w[1]=2, and w[2]=3 and then compare weights instead of values in the algorithm, but is it possible to do without this - I want to apply the algorithm to a problem where this

distinct permutations of a list with repetitions

我的梦境 提交于 2021-01-27 19:26:26
问题 I want to write an R code to generate all distinct permutations of a list with a repeated characters in an efficient way. For example, x<-c(1,1,2,2,3,4); library(combinat); unique(permn(x)) works, but it is very inefficient and dose not work if the length of the vector x is a bit longer. Does anybody know how to generate the unique permutations of above sequence in an efficient way? 回答1: Permutations are unwieldy beasts. The number of permutations you get when selecting r objects from a set