Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在NetLogo中生成列表的排列_List_Permutation_Netlogo - Fatal编程技术网

List 在NetLogo中生成列表的排列

List 在NetLogo中生成列表的排列,list,permutation,netlogo,List,Permutation,Netlogo,我试图在NetLogo中生成一个列表,其中包含几个不同的数字0到n的唯一列表。例如,我有这行代码 set mylists [[0 1 2] [0 2 1] [1 0 2] [1 2 0] [2 0 1] [2 1 0]] 我写这篇文章是为了让所有可能的0 1和2的唯一组合都不会重复列表中的数字。我希望能够做同样的事情,但有一个更大的n。有没有一个例子说明如何做到这一点,或者有人知道的某种伪代码算法,我可以检查一下?谢谢 如果您不介意递归解决方案,您可以这样做: to-report permut

我试图在NetLogo中生成一个列表,其中包含几个不同的数字0到n的唯一列表。例如,我有这行代码

set mylists [[0 1 2] [0 2 1] [1 0 2] [1 2 0] [2 0 1] [2 1 0]]

我写这篇文章是为了让所有可能的0 1和2的唯一组合都不会重复列表中的数字。我希望能够做同样的事情,但有一个更大的n。有没有一个例子说明如何做到这一点,或者有人知道的某种伪代码算法,我可以检查一下?谢谢

如果您不介意递归解决方案,您可以这样做:

to-report permutations [#lst] ;Return all permutations of `lst`
  let n length #lst
  if (n = 0) [report #lst]
  if (n = 1) [report (list #lst)]
  if (n = 2) [report (list #lst reverse #lst)]
  let result []
  let idxs n-values n [?]
  foreach idxs [
    let xi item ? #lst
    foreach (permutations remove-item ? #lst) [
      set result lput (fput xi ?) result
    ]
  ]
  report result
end
编辑:更新语法以响应评论

to-report permutations [#lst] ;Return all permutations of `lst`
  let n length #lst
  if (n = 0) [report #lst]
  if (n = 1) [report (list #lst)]
  if (n = 2) [report (list #lst reverse #lst)]
  let result []
  let idxs range n
  foreach idxs [? ->
    let xi item ? #lst
    foreach (permutations remove-item ? #lst) [?? ->
      set result lput (fput xi ??) result
    ]
  ]
  report result
end

fwiw,目前在许多编程语言中都有解决方案。可能方案一最容易移植到NetLogo