我正在尝试编写一个执行以下操作的函数:
下面的函数(我在网上找到)通过将一个字符串作为参数,然后返回该字符串的所有排列来完成此操作
我无法弄清楚如何修改它以使其与整数数组一起使用(我认为这与某些方法在字符串上的工作方式不同于它们在整数上的工作方式有关,但我不确定. ..)
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
};
Run Code Online (Sandbox Code Playgroud)
注意:我希望使函数返回整数数组,而不是字符串数组.
我真的需要使用JavaScript的解决方案.我已经在python中找到了如何做到这一点
说我有一个n个元素的列表,我知道有n个!订购这些元素的可能方式.生成此列表的所有可能排序的算法是什么?例如,我有列表[a,b,c].该算法将返回[[a,b,c],[a,c,b,],[b,a,c],[b,c,a],[c,a,b],[c,b , 一个]].
我在这里阅读 http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations
但维基百科从未擅长解释.我不太了解它.
我正在研究Data.List模块中的代码,并不能完全围绕这种排列实现:
permutations :: [a] -> [[a]]
permutations xs0 = xs0 : perms xs0 []
where
perms [] _ = []
perms (t:ts) is = foldr interleave (perms ts (t:is)) (permutations is)
where interleave xs r = let (_,zs) = interleave' id xs r in zs
interleave' _ [] r = (ts, r)
interleave' f (y:ys) r = let (us,zs) = interleave' (f . (y:)) ys r
in (y:us, f (t:y:us) : zs)
Run Code Online (Sandbox Code Playgroud)
有人可以详细解释这些嵌套函数如何相互连接/相互作用?
我有一个旋转列表的功能:
rotate :: [a] -> [a]
rotate [] = []
rotate (x:xs) = xs ++ [x]
Run Code Online (Sandbox Code Playgroud)
现在我想要一个函数,它给出一个包含有限列表的每个可能旋转的列表:
rotateAll :: [a] -> [[a]]
Run Code Online (Sandbox Code Playgroud)
在命令式语言中,我会做(像伪代码)
for i = 1 to length of list
append list to rotateList
list = rotate(list)
Run Code Online (Sandbox Code Playgroud)
当然,强制性思考可能无法帮助我找到解决这个问题的功能.我正在寻找一些如何解决这个问题的提示.
其他想法:
要解决这个问题,我有两个问题需要解决.首先,我需要重复旋转列表并将每个结果收集到一个列表中.所以第一个解决方案需要做类似的事情
rotateAll xs = [xs (rotate xs) (rotate (rotate xs)) (rotate (rotate (rotate xs))) ...]
Run Code Online (Sandbox Code Playgroud)
当然我不知道要做多少次.我会满意地无限地做到这一点然后take (length xs)用来获得我想要的有限数量的列表.这实际上证明了第二个问题:确定何时停止.我不知道使用take是否是解决问题的最有效或最优雅的方式,但是当我输入它并且应该工作时,它会浮现在脑海中.
附录: 现在我已经找到了两个解决方案或者提示.我很乐意欢迎任何其他更快或使用不同方法的解决方案.谢谢!
我已经有了生成元素列表的所有排列的代码。然而,我意识到,如果我想操作生成的列表,我需要遍历这个列表。该列表可能会很大,因此维护成本很高。我想知道是否有一种方法可以通过每次调用生成排列,以便我可以检查列表是否与我需要的匹配,如果不匹配,我将生成下一个排列。(每次该函数都会一次返回一个列表。)
我的代码:
(defun allPermutations (list)
(cond
((null list) nil)
((null (cdr list)) (list list))
(t (loop for element in list
append (mapcar (lambda (l) (cons element l))
(allPermutations (remove element list)))))))
Run Code Online (Sandbox Code Playgroud) How can I edit the following code to make Haskell show all the possibilities of rotating an input list from the user :
rotate :: Int -> [a] -> [a]
rotate n text = take (length text) (drop n (cycle text))
Run Code Online (Sandbox Code Playgroud)
I assume that to print all the possibilities we need to drop the first element X times. where X is the length of the list entered.
circle :: [a] -> [[a]]
circle text = take (length text) (drop (1) …Run Code Online (Sandbox Code Playgroud) list ×5
permutation ×4
haskell ×3
algorithm ×1
common-lisp ×1
generator ×1
javascript ×1
lisp ×1