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!