Re: Get all combinations of n digits among m digits

new topic     » goto parent     » topic index » view thread      » older message » newer message

Damned, there was still an error, always due to the position of this test "if nr=1 then":

sequence combis = {} 
 
procedure recurse(sequence remaining,               /* digits remaining to compute */ 
                  integer needed,                   /* number of digits needed for a valid combination */ 
                  sequence selected = {})           /* digits already selected for a potential combination */ 
  integer nr = length(remaining)   -- number of digits remaining to compute 
  integer ns = length(selected)    -- number of digits already selected for a potential combination 
  if ns+nr = needed then 
    combis = append(combis, selected & remaining) 
    return 
  end if 
  if ns = needed then 
    combis = append(combis, selected) 
    return 
  end if 
  if nr = 1 then return end if 
  -- select next 
  recurse(remove(remaining, 1), needed, selected & remaining[1]) 
  -- skip next 
  recurse(remove(remaining, 1), needed, selected) 
end procedure 
 
function combinations(sequence s, integer lg) 
  combis = {} 
  recurse(s, lg) 
  return combis  
end function   

Jean-Marc

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu