How to form subset form a set of numbers in Netlogo -
dear netlogo community,
i looking generate subset of set of numbers. example if set [1 2 3 4 5] subset [1 2] [1 3] [1 4] [1 5] [ 1 2 3] [1 2 4]....... know can generate using bit manipulation in java. have no idea how implement in netlogo. doomed. appreciated.
this easiest solve using recursion:
to-report subsets [xs] if empty? xs [ report [[]] ] let recurse subsets butfirst xs report sentence recurse map [fput first xs ?] recurse end the basic idea if want subsets of [1 2 3], first find subsets of [2 3]. of them subsets of [1 2 3], plus if stick 1 on front of each of them, resulting lists part of answer too.
sample run:
observer> print subsets [1 2 3] [[] [3] [2] [2 3] [1] [1 3] [1 2] [1 2 3]]
Comments
Post a Comment