Date: Fri Oct 3 12:46:44 MST 2003 From: Joseph Bergevin <joseph@bergevin.com> Subject: Algorithm for creation of all possible subsets of a given setIf you ever need to create a list of all possible subsets of a given list, this is what I came up with. Here's an example from the message window:
put get_permutations([1,2,3])
-- [[1], [1, 2], [1, 3], [1, 2, 3], [2], [2, 3], [3]]
The method accepts a list as the argument, and returns a list of lists. It works quickly for lists up to 100 elements, and only slows linearly. Here's the code:
----get-permutations------Joe-Bergevin----------
on get_permutations n
psets=[]
repeat with y=1 to n.count
psets.add([n[y]])
repeat with span=2 to n.count
repeat with x=y to n.count-span+1
pset=[]
pset.add(n[y])
repeat with i=x+1 to x+span-1
pset.add(n[i])
end repeat
psets.add(pset)
end repeat
end repeat
end repeat
return(psets)
end
---------------------------------------