permutation

How to convert this function from recursive to iterative?

末鹿安然 提交于 2020-06-01 05:50:29
问题 I have this method: static List<string> permutation(List<string> lst) { switch (lst.Count) { case 0: case 1: return lst; default: List<string> l = new List<string>(); for (int i = 0; i < lst.Count; i++) { string m = lst[i]; List<string> remLst = lst.Except(new List<string> { m }).ToList(); List<string> tmp = permutation(remLst); for (int i2 = 0; i2 < tmp.Count; i2++) { l.Add(m + tmp[i2]); } } return l; } } which gives all permutations of the list lst . The idea is to one by one extract all

permutations in Javascript

℡╲_俬逩灬. 提交于 2020-05-30 08:13:56
问题 I'm trying to write a function in Javascript that can return the number of permutations, and also show all the permutations of a string (suppose that non of the character is repeated) using recursive methods. I've seen a lot using for loop, but is there a way that I can obtain the same outcome without using it? For the number of permutations, here is my attempt without using for loop var permutation = function (s) { var fac = function (t) { if (t === 0) return 1; return t*fac(t-1); }; return

permutations in Javascript

拜拜、爱过 提交于 2020-05-30 08:13:46
问题 I'm trying to write a function in Javascript that can return the number of permutations, and also show all the permutations of a string (suppose that non of the character is repeated) using recursive methods. I've seen a lot using for loop, but is there a way that I can obtain the same outcome without using it? For the number of permutations, here is my attempt without using for loop var permutation = function (s) { var fac = function (t) { if (t === 0) return 1; return t*fac(t-1); }; return

Permutations with repetition without two consecutive equal elements

浪子不回头ぞ 提交于 2020-04-13 15:37:46
问题 I need a function that generates all the permutation with repetition of an iterable with the clause that two consecutive elements must be different; for example f([0,1],3).sort()==[(0,1,0),(1,0,1)] #or f([0,1],3).sort()==[[0,1,0],[1,0,1]] #I don't need the elements in the list to be sorted. #the elements of the return can be tuples or lists, it doesn't change anything Unfortunatly itertools.permutation doesn't work for what I need (each element in the iterable is present once or no times in

python实现克莱姆法则

拟墨画扇 提交于 2020-03-05 20:25:55
文章目录 首先完成python模拟行列式运算 公式分析 模块分析与实现 环境 模块导入 全排列 逆序数 方阵计算 克莱姆法则 *Cramer's rule* 注:本文对numpy对象使用append方法时均使用了深拷贝deepcopy,因为python中对象的赋值是按引用传递的,如果不使用深拷贝在append时会改变原有对象从而覆盖原先的值 首先完成python模拟行列式运算 ∣ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a n 1 a n 2 ⋯ a n n ∣ = ∑ ( − 1 ) t a 1 p 1 a 2 p 2 ⋯ a n p n \begin{vmatrix} {a_{11}}&{a_{12}}&{\cdots}&{a_1n}\\ {a_{21}}&{a_{22}}&{\cdots}&{a_2n}\\ {\vdots}&{\vdots}&{\ddots}&{\vdots}\\ {a_{n1}}&{a_{n2}}&{\cdots}&{a_{nn}}\\ \end{vmatrix}= \sum{(-1)^{t}}{a_{1p_{1}}a_{2p_{2}}}{\cdots}{a_{np_{n}}} ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ∣ ​ a 1 1 ​ a 2 1 ​ ⋮ a n 1 ​ ​ a 1 2 ​ a 2 2 ​ ⋮

Combinations of expressions with 4 elementary operations

為{幸葍}努か 提交于 2020-03-01 06:45:25
问题 I could not come up with a better title, for an adequate one might require the whole explanation. Also, combinations could be misleading since the problem will involve permutations. What I want to accomplish is to outperform a brute force approach in Python at the following problem: Given the 4 elementary operations [+,-,*,/] and the digits from 1 to 9, and given all the possible combinations of 5 digits and the 4 operations without repetition (permutations) that result in a given number

JavaScript - Generating combinations from n arrays with m elements

情到浓时终转凉″ 提交于 2020-02-24 12:22:28
问题 I'm having trouble coming up with code to generate combinations from n number of arrays with m number of elements in them, in JavaScript. I've seen similar questions about this for other languages, but the answers incorporate syntactic or library magic that I'm unsure how to translate. Consider this data: [[0,1], [0,1,2,3], [0,1,2]] 3 arrays, with a different number of elements in them. What I want to do is get all combinations by combining an item from each array. For example: 0,0,0 // item

all permutations of string without using itertools

走远了吗. 提交于 2020-02-07 07:01:32
问题 All possible strings of any length that can be formed from a given string Input: abc Output: a b c abc ab ac bc bac bca cb ca ba cab cba acb I have tried using this but it's limited to string abc, I want to generalize it like if input 'abcd' i will provide me output for the same. def perm_main(elems): perm=[] for c in elems: perm.append(c) for i in range(len(elems)): for j in range(len(elems)): if perm[i]!= elems[j]: perm.append(perm[i]+elems[j]) level=[elems[0]] for i in range(1,len(elems)):

Combination of a Collection with Repetitions

好久不见. 提交于 2020-02-06 04:19:06
问题 There are a lot of links on http://stackoverflow.com for how to do combinations: Generating combinations in c++ But these links presume to draw from an infinite set without repetition. When given a finite collection which does have repetition, these algorithms construct duplicates. For example you can see the accepted solution to the linked question failing on a test case I constructed here: http://ideone.com/M7CyFc Given the input set: vector<int> row {40, 40, 40, 50, 50, 60, 100}; I expect

Combination of a Collection with Repetitions

江枫思渺然 提交于 2020-02-06 04:19:05
问题 There are a lot of links on http://stackoverflow.com for how to do combinations: Generating combinations in c++ But these links presume to draw from an infinite set without repetition. When given a finite collection which does have repetition, these algorithms construct duplicates. For example you can see the accepted solution to the linked question failing on a test case I constructed here: http://ideone.com/M7CyFc Given the input set: vector<int> row {40, 40, 40, 50, 50, 60, 100}; I expect