1

i´m new to Netlogo as well as programming and struggling on the following problem: I need to implement a function Permu [k,n] in Netlogo where k is a list of elements e.g [1 2] while n is the length of the resulting list e.g. 3. The function should return all permutation say lists with length n where the elements are taken from k.

Example 1: Permu [1 2] 3 should return as a result all permutations with repetition [[1 1 1] [1 1 2] [1 2 1] [1 2 2] [2 1 1] [2 1 2] [2 2 1] [2 2 2]]

Example 2: Permu [1 2 3] 2 should return [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3] [3 1] [3 2] [3 3]]

Actually, the equivalent function in Mathematica would be Tuples[{1,2},3]

I already checked post Generating permutations of a list in NetLogo, but was unable to adjust it to my needs.

Do you have any ideas how to do this or know some kind of pseudcode? Thanks in advance!

Edit

I came up with a (bad) solution by simply creating random lists made up of elements from k until all possible combinations are found:

to-report permu [k n]

  let number-of-all-combinations ( length k ) ^ n ;; stopping condtion

  let results []
  let temp-results []

  while [ length results < number-of-all-combinations ] [

    set temp-results []
    repeat n [ set temp-results fput (one-of k) temp-results ]
    if not member? temp-results results [set results fput temp-results results ] ;; makes sure that there are no duplicates in the result.
  ]

  report results

end

I would still appreciate a better alternative.Thanks!

geruter
  • 386
  • 1
  • 6
  • What exactly means " unable to adjust". What errors showed up or what permutations were missing in your result set. A basic approach which we can improve together with you is really appreciated! – mle Mar 17 '19 at 14:36
  • 1
    The function described there generated permutations of a given list without repetition e.g. Permu [ 1 2 ] returned [ [ 1 2 ] [ 2 1 ] ] while [1 1] and [2 2] where missing. But you are right about providing a basic approach, please check the edit. Sorry, first time for me! – geruter Mar 17 '19 at 16:24
  • 1
    No problem, now it's great for us all to start thinking! Don't worry, we all started some day here with our first question! :-) – mle Mar 17 '19 at 19:13

0 Answers0