Post Reply 
Symbolic Combinations and Permutations question.
01-13-2015, 02:53 AM (This post was last modified: 01-13-2015 03:53 AM by Han.)
Post: #4
RE: Symbolic Combinations and Permutations question.
Here's my attempt at the general version via recursion. Usage:

COMBOSET(list,n)

Works for arbitrary sets, too. For example, try: COMBOSET({"a","b",1,[0,-1,0]},2)

Code:
EXPORT COMBOSET(s,n)
BEGIN
  local l:={};
  local sl:={};
  local tl:={};
  local i,j;
  local m:=size(s);

  case
    if n==m then l:={s}; end;
    if n==1 then l:=makelist({s(X)},X,1,m); end; 
    if n>m then l:={}; end;
    for i from 2 to m-n+2 do
      sl:=sub(s,i,m); 
      tl:=COMBOSET(sl,n-1);
      for j from 1 to size(tl) do
        tl(j):=concat({s(i-1)},tl(j));
      end;
      l:=concat(l,tl); 
    end;
  end;

  return(l);

END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Symbolic Combinations and Permutations question. - Han - 01-13-2015 02:53 AM



User(s) browsing this thread: 1 Guest(s)